我正在尝试比较两个列表的内容-
一个列表包含文件名,即
filenamestring = ['a', 'b']
另一个列表包含多个单词及其得分,即
outlist = [('c', 46), ('d',
40), ('e', 37), ('f', 35),('a', 40),('b', 37),('c', 1), ('d',
2), ('e', 3), ('f', 4)]
我正在尝试从2个列表中找到匹配的单词,如果匹配正确,则将分数添加为200
尝试了下面的代码,但没有任何反应-
for letter, number in outlist:
if word in filenamestring == letter in outlist:
output[letter] = number + 200
else:
output[letter] += number
输出:
output = [('c', 47), ('d',42), ('e', 40), ('f', 39),('a', 240),('b', 237)]
答案 0 :(得分:2)
for letter, number in outlist:
if letter in filenamestring: # directly check if letter is in the filenamestring
output[letter] = number + 200
else:
output[letter] += number
答案 1 :(得分:1)
工作代码:
filenamestring = ["b", "a"]
# The other list contains multiple words and their scores i.e
outlist = [('c', 46), ('d', 40), ('e', 37), ('f', 35),('a', 40),('b', 37)]
# I'm trying to find matching words from the 2 list and if matched correctly add the score with a value of 200
# Tried the code below but nothing happened-
i = 0
for letter, number in outlist:
print(letter + "= " + str(number))
for word in filenamestring:
if word == letter :
outlist[i] =(letter, number + 20000000)
break
else:
outlist[i] = (letter, number)
i= i+1
print(outlist)
答案 2 :(得分:1)
您可以使用列表推导:
output = [(i[0], i[1]+200) if i[0] in filenamestring else (i[0],i[1]) for i in outlist]