Python for循环计数器列表-计数器不工作
有人能帮我解决代码故障吗。 我有两张单子Python for循环计数器列表-计数器不工作,python,list,for-loop,Python,List,For Loop,有人能帮我解决代码故障吗。 我有两张单子 A = [['2925750', ' Everything he mentioned, I could have evaluated on my own'], ['2925750', ' I do wish he could have shown us more at this point that could have set the fox apart.']] B = ['mentioned','evaluated','fox','wish
A = [['2925750', ' Everything he mentioned, I could have evaluated on my own'], ['2925750', ' I do wish he could have shown us more at this point that could have set the fox apart.']]
B = ['mentioned','evaluated','fox','wish']
目标是将B中的任何项目在A的句子中出现的次数附加到列表A中
结果应该是:
[(['2925750', ' Everything he mentioned, I could have evaluated on my own'], 0), (['2925750', ' I do wish he could have shown us more at this point that could have set the Equinox apart.'], 0)]
问题是我的计数是零
下面是我的代码。提前感谢您:
Y = []
##Find Matches
for sent in A:
for sent in B:
Y.append((sent, sum(sent.count(col) for col in B)))
谢谢。为B发送的变量盖过了为A发送的变量。或者更准确地说,它为相同的变量指定了相同的名称
您应该重命名其中一个
另外,请注意,您已经在sum中迭代了B。在内部循环中,您可能希望迭代A中的每个列表
为B发送的变量盖过了为A发送的变量。或者,更准确地说,它将相同的变量分配给相同的名称
您应该重命名其中一个
另外,请注意,您已经在sum中迭代了B。在内部循环中,您可能希望迭代A中的每个列表
不能将sent用作两个循环的迭代器,因为它仍在作用域中。不能将sent用作两个循环的迭代器,因为它仍在作用域中
你用了两次“发送”
B不需要循环
“A”是列表的列表
我的解决方案:
Y = []
A = [['2925750', ' Everything he mentioned, I could have evaluated on my own'], ['2925750', ' I do wish he could have shown us more at this point that could have set the fox apart.']]
B = ['mentioned','evaluated','fox','wish']
for sent in A:
o = 0
for i in sent:
o +=sum(i.count(col) for col in B)
Y.append((sent, o))
Y:
他提到的每一件事,我都可以根据自己的经验进行评估
own'],2,['2925750',我真希望他能在
这一点可能会让狐狸与众不同
你用了两次“发送”
B不需要循环
“A”是列表的列表
我的解决方案:
Y = []
A = [['2925750', ' Everything he mentioned, I could have evaluated on my own'], ['2925750', ' I do wish he could have shown us more at this point that could have set the fox apart.']]
B = ['mentioned','evaluated','fox','wish']
for sent in A:
o = 0
for i in sent:
o +=sum(i.count(col) for col in B)
Y.append((sent, o))
Y:
他提到的每一件事,我都可以根据自己的经验进行评估
own'],2,['2925750',我真希望他能在
这一点可能会让狐狸与众不同
工作得很好。非常感谢你!工作得很好。非常感谢你!