我有两个元组列表,其中每个列表中的元组都是唯一的。 列表具有以下格式:
[('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 agianst col2,col3的一个项目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或表格的其他所有项目的元组列表项目?
谢谢
i have two lists of tuples, where tuples in the each list are all unique. lists have the following format:
[('col1', 'col2', 'col3', 'col4'), ...]i'm using a nested loop to find the members from both lists that have the same values for given cols, col2 and 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)simply working. but it takes many minutes to complete when there are tens of thousands of items in lists.
Using tabular, i can filter list1 agianst col2, col3 of one item for list2 as given below:
list1 = tb.tabular(records=[...], names=['col1','col2','col3','col4']) ... for (col1, col2, col3, col4) in list2: list1[(list1['col2'] == col2) & (list1['col3'] == col3)]which is obviously "doing it wrong" and way much slower than the first.
how can i effectively check items of a list of tuples against all the items of another using numpy or tabular?
thanks
最满意答案
尝试这个:
temp1 = set([]) temp2 = set([]) dict1 = dict() dict2 = dict() for key, value in zip([tuple(l[1:3]) for l in list1], list1): dict1.setdefault(key, list()).append(value) for key, value in zip([tuple(l[1:3]) for l in list2], list2): dict2.setdefault(key, list()).append(value) for key in dict1: if key in dict2: temp1.update(dict1[key]) temp2.update(dict2[key])肮脏的,但应该工作。
Try this:
temp1 = set([]) temp2 = set([]) dict1 = dict() dict2 = dict() for key, value in zip([tuple(l[1:3]) for l in list1], list1): dict1.setdefault(key, list()).append(value) for key, value in zip([tuple(l[1:3]) for l in list2], list2): dict2.setdefault(key, list()).append(value) for key in dict1: if key in dict2: temp1.update(dict1[key]) temp2.update(dict2[key])Dirty one, but should work.
更多推荐
发布评论