通过将所有项目与numpy或表格进行比较来过滤两个列表(Filtering two lists by comparing all items to eachother with numpy or ta

编程入门 行业动态 更新时间:2024-10-21 18:30:13
通过将所有项目与numpy或表格进行比较来过滤两个列表(Filtering two lists by comparing all items to eachother with numpy or tabular)

我有两个元组列表,其中每个列表中的元组都是唯一的。 列表具有以下格式:

[('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.

更多推荐

本文发布于:2023-04-29 01:22:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1334540.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:表格   两个   项目   列表   Filtering

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!