因此,我试图编写代码来比较两个列表(firstfile from firstfile.txt;secondfile from secondfile.txt(,并打印来自firstfile的重叠单词在secondfile中出现的次数。
例如,如果在第一个文件(.txt(中,我有:
'beautiful', 'day', 'neighborhood', 'sun'
在第二个文件(.txt(中,我有
"It's a beautiful day in the neighborhood today. The sun is shining brightly, and the birds are singing.
And now I run into this beautiful lady, whose skin gleams in the sun light."
所以我所期望的结果是:
beautiful: 2
day: 1
neighborhood: 1
sun: 2
到目前为止,我已经提出了以下内容:
results = {}
for i in firstfile:
results[i] = secondfile.count(i)
print(results)
但它能打印出类似的东西
{'beautiful': 0, 'day': 0, 'neighborhood': 0, 'sun': 0}
这显然是不正确的。
我做错了什么?我尝试了十几种不同的方法,但对于重叠的单词,除了0之外,似乎没有一种返回任何其他值。代码有问题吗?或者我应该以特定的方式从firstfile.txt和secondfile.txt创建列表吗?谢谢大家!(此外,我是Python(以及一般编程(的新手,所以如果这是一个愚蠢的问题,或者我没有把事情说清楚,请原谅我!!(
您需要将firstfile声明为列表:
firstfile = ['beautiful', 'day', 'neighborhood', 'sun']
secondfile = "It's a beautiful day in the neighborhood today. The sun is shining brightly, and the birds are singing. And now I run into this beautiful lady, whose skin gleams in the sun light."
results = {}
for i in firstfile:
results[i] = secondfile.count(i)
这对我有用。