我的代码中有两个for循环问题:
def batchm():
searchFile = sys.argv[1]
namesFile = sys.argv[2]
writeFile = sys.argv[3]
countDict = {}
with open(searchFile, "r") as nlcfile:
with open(namesFile, "r") as namesList:
with open(writeFile, "a") as wfile:
for name in namesList:
for line in nlcfile:
if name in line:
res = line.split("\t")
countValue = res[0]
countKey = res[-1]
countDict[countKey] = countValue
countDictMax = sorted(countDict, key = lambda x: x[1], reverse = True)
print(countDictMax)
循环正在迭代:
namesList:
格林
唐纳德
唐老鸭
麦克唐纳
。 。
nlcfile:
123 1999-2000 Northampton Town F.C.季节北安普顿镇
5 John Simpson Kirkpatrick
167 File talk:NewYorkRangers1940s.png talk
234 Parshu Ram Sharma(Raj Comics)Parshuram Sharma
。 。
我得到的是这样的:
['Lyn Greene\n', 'Rydbergia grandiflora (Torrey & A. Gray in A. Gray) E. Greene\n', 'Tyler Greene\n', 'Ty Greene\n' ..... ]
并且此列表出现48次,这也恰好是namesList中的行数。
期望的输出:
("string from namesList" -> "record with highest number in nlcfile")
Greene -> Ly Greene
Donald -> Donald Duck
。 。 。
我认为这两个for循环不会以正确的方式迭代。但我不知道为什么。
任何人都可以看到,问题在哪里?
非常感谢!