【发布时间】:2011-05-26 19:35:25
【问题描述】:
我有两个元组列表,每个列表中的元组都是唯一的。 列表具有以下格式:
[('col1', 'col2', 'col3', 'col4'), ...]
我正在使用嵌套循环从两个列表中查找对于给定 cols、col2 和 col3 具有相同值的成员
temp1 = set([])
temp2 = set([])
for item1 in list1:
for item2 in list2:
if item1['col2'] == item2['col2'] and \
item1['col3'] == item2['col3']:
temp1.add(item1)
temp2.add(item2)
只是工作。但是当列表中有数万个项目时,需要几分钟才能完成。
使用表格,我可以过滤 list1 和 col2,list2 的一项,如下所示:
list1 = tb.tabular(records=[...], names=['col1','col2','col3','col4'])
...
for (col1, col2, col3, col4) in list2:
list1[(list1['col2'] == col2) & (list1['col3'] == col3)]
这显然是“做错了”并且比第一个慢得多。
如何使用 numpy 或表格有效地检查元组列表中的项目与另一个项目的所有项目?
谢谢
【问题讨论】: