我们有两个列表,发现如果列表的列表中有从另一个列表中的项目
l=["a","b","c"]
s=[["a","b","c"],
["a","d","c"],
["a-B1","b","c"],
["a","e","c"],
["a_2","c"],
["a","d-2"],
["a-3","b","c-1-1","d"]]
print l
print s
现在,我尝试看看如果s
各2级列表中有模糊匹配任何在列表中的项目l
。
matches=list()
matchlist2=list()
print s2
for i in range(len(s)):
matches.append([])
for j in range(len(s[i])):
for x in l:
if s[i][j].find(x)>=0:
print s[i][j]
matches[i].append(True)
break
else:
matches[i].append(False)
matchlist2.append(all(x for x in matches[i]))
print matches
print matchlist2
这给了我想要的东西。但我对它有如此多的循环感到不满意。我也在和大熊猫一起工作,如果有大熊猫的解决方案,那将是很棒的。在熊猫中,只有两列两个数据框。
[[True, True, True], [True, False, True], [True, True, True], [True, False, True], [True, True], [True, False], [True, True, True, False]]
第二个代码检查子列表中的所有项目是否匹配。
[True, False, True, False, True, False, False]
我相信你正在寻找http://www.codereview.stackexchange.com。我们在这里的代码是*不*工作。 – zondo
在你的情况下,什么被定义为模糊匹配? – timgeb
在@zondo说的之上,看看递归。它将减少循环层次并为您提供多层次数组的更多功能。 – CalebB