为列表中的单词分配分数
Assign a score to a word in a list
我有一本字典和一个包含单词的子列表的列表:
d={"love":3, "good":2}
l=[["love", "apple", "same"],["good", "GOOD", "year"]]
如果列表中的单词不在字典键中,我需要为该单词分配一个值 3,并对子列表的值求和。
在示例中,值的结果将是:
[6, 3]
我试过这个代码:
def scoring():
score1=[]
for sublst in l:
for val in sublst:
if val.lower() not in d.get(val.lower(), 0):
score1=sum(3 for val in sublst)
print(score1)
以下列表理解将提供预期的结果:
>>> [sum(3 if w.lower() not in d else 0 for w in sl) for sl in l]
[6, 3]
更正问题中的函数:
def scoring():
score1=[]
for sublst in salida_tweets_separado:
s = 0
for val in sublst:
if val.lower() not in valores:
s += 3
score1.append(s)
print(score1)
我有一本字典和一个包含单词的子列表的列表:
d={"love":3, "good":2}
l=[["love", "apple", "same"],["good", "GOOD", "year"]]
如果列表中的单词不在字典键中,我需要为该单词分配一个值 3,并对子列表的值求和。 在示例中,值的结果将是:
[6, 3]
我试过这个代码:
def scoring():
score1=[]
for sublst in l:
for val in sublst:
if val.lower() not in d.get(val.lower(), 0):
score1=sum(3 for val in sublst)
print(score1)
以下列表理解将提供预期的结果:
>>> [sum(3 if w.lower() not in d else 0 for w in sl) for sl in l]
[6, 3]
更正问题中的函数:
def scoring():
score1=[]
for sublst in salida_tweets_separado:
s = 0
for val in sublst:
if val.lower() not in valores:
s += 3
score1.append(s)
print(score1)