简体   繁体   English

如何将 append 项目添加到 python 中的 for 循环内的列表中?

[英]How do I append items to a list inside a for loop in python?

What I want to do is take names from namelist, compare those to names in banklist, and if there is an item in banklist which looks a lot like an item in namelist, I want to append that item to a closematchlist.我想要做的是从名单中取名字,将它们与银行列表中的名称进行比较,如果银行列表中有一个项目看起来很像名单中的项目,我想将该项目 append 到 closematchlist。 The goal of this is to find items that occur in both lists, even if there is a spelling error in namelist.这样做的目的是查找出现在两个列表中的项目,即使名称列表中存在拼写错误。 When I print(closematch), it works like intended: the close matches in banklist get found and printed.当我打印(closematch)时,它按预期工作:找到并打印banklist中的密切匹配。 However, when I try to append those items to a list, the result of print(closematchlist) is [].但是,当我尝试将 append 这些项目添加到列表时,print(closematchlist) 的结果是 []。

for name in namelist:
    closematch = difflib.get_close_matches(name, banklist, 1, 0.8)
    closematchlist = list()
    closematchlist.append(closematch)
    print(closematch)
print(closematchlist)```

difflib.get_close_matches() is a list of close matches. difflib.get_close_matches()是紧密匹配的列表。 You don't need to copy it to a new list.您无需将其复制到新列表中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM