在 python 中搜索嵌套列表

发布于 2024-11-16 09:18:33 字数 202 浏览 4 评论 0原文

我有一个包含 97510 个值的元组的嵌套列表,如下所示:

a = [ (1,2,3), (3,4,5), (5,4,2)]

每个第一个值(索引 = 0)都是唯一的,我需要找到具有相同索引 = 1 项的其他索引 = 0 项 在示例中,我需要找到第二个和第三个元组,其中第二项“4”是常见的。

我该怎么做?

I have a nested list of tuples of 97510 values like this:

a = [ (1,2,3), (3,4,5), (5,4,2)]

every first value (index=0) is unique and I need to find other index=0 items that have the same index=1 items
In the example , I need to find the second and third tuples where the the second item '4' is common .

How do I do it ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

抽个烟儿 2024-11-23 09:18:34

如果您想查找所有匹配项:

>>> from collections import defaultdict
>>> d = defaultdict(list)
>>> for inner in a:
...     d[inner[1]].append(inner)
... 
>>> d
defaultdict(<type 'list'>, {2: [(1, 2, 3)], 4: [(3, 4, 5), (5, 4, 2)]})
>>> d[4]
[(3, 4, 5), (5, 4, 2)]

如果您想挑选特定第二个值的所有匹配项:

>>> filter(lambda inner: inner[1] == 4, a)
[(3, 4, 5), (5, 4, 2)]

编辑:如注释,列表理解是更好的选择,因为它对于此类工作更有效:

>>> [inner for inner in a if inner[1] == 4]
[(3, 4, 5), (5, 4, 2)]

使用 timeit 显示列表理解速度大约快 2.5 倍(无论如何在我的机器上):

>>> timeit.timeit('[inner for inner in a if inner[1] == 4]', 'a=[(1,2,3), (3,4,5), (5, 4, 2)]')
2.5041549205780029
>>> timeit.timeit('filter(lambda inner: inner[1] == 4, a)', 'a=[(1,2,3), (3,4,5), (5, 4, 2)]')
6.328679084777832

If you want to find all matches:

>>> from collections import defaultdict
>>> d = defaultdict(list)
>>> for inner in a:
...     d[inner[1]].append(inner)
... 
>>> d
defaultdict(<type 'list'>, {2: [(1, 2, 3)], 4: [(3, 4, 5), (5, 4, 2)]})
>>> d[4]
[(3, 4, 5), (5, 4, 2)]

If you want to pick out all matches for a particular second value:

>>> filter(lambda inner: inner[1] == 4, a)
[(3, 4, 5), (5, 4, 2)]

Edit: As pointed out in the comments, a list comprehension is preferable as it is more efficient for such work:

>>> [inner for inner in a if inner[1] == 4]
[(3, 4, 5), (5, 4, 2)]

Using timeit shows the list comprehension is about 2.5 times faster (on my machine anyway):

>>> timeit.timeit('[inner for inner in a if inner[1] == 4]', 'a=[(1,2,3), (3,4,5), (5, 4, 2)]')
2.5041549205780029
>>> timeit.timeit('filter(lambda inner: inner[1] == 4, a)', 'a=[(1,2,3), (3,4,5), (5, 4, 2)]')
6.328679084777832
唯憾梦倾城 2024-11-23 09:18:34

这是一种方法:

>>> result = defaultdict(list)
>>> for item in a:
>>>     result[item[1]].append(item)
>>> result
defaultdict(<type 'list'>, {2: [(1, 2, 3)], 4: [(3, 4, 5), (5, 4, 2)]})

这将生成一个列表字典,其中具有相同第二个值的所有项目都在一个列表中,并以该值作为键。

Here is one way to do it:

>>> result = defaultdict(list)
>>> for item in a:
>>>     result[item[1]].append(item)
>>> result
defaultdict(<type 'list'>, {2: [(1, 2, 3)], 4: [(3, 4, 5), (5, 4, 2)]})

This will result in a dictionary of lists where all items with the same second value are in one list, with that value as the key.

半城柳色半声笛 2024-11-23 09:18:34

另一种选择:

from operator import itemgetter
from itertools import groupby

a = [ (1,2,3), (3,4,5), (5,4,2)]
b = groupby(sorted(a), itemgetter(1))
for val, group in b:
    print val, list(group)
# 2 [(1, 2, 3)]
# 4 [(3, 4, 5), (5, 4, 2)]

Another alternative:

from operator import itemgetter
from itertools import groupby

a = [ (1,2,3), (3,4,5), (5,4,2)]
b = groupby(sorted(a), itemgetter(1))
for val, group in b:
    print val, list(group)
# 2 [(1, 2, 3)]
# 4 [(3, 4, 5), (5, 4, 2)]
昇り龍 2024-11-23 09:18:34

请注意,您还可以使用 groupby

from itertools import groupby

data = [ (1,2,3), (3,4,5), (5,4,2)]
res = groupby(sorted(data), key=lambda x: x[1])

根据评论

进行 编辑问题并找到了另一种解决方案 - 但不是最好的解决方案,但是:

inputVals = [(1,2,3), (3,4,5), (5,4,2), (2,2,3), (7,3,1)]
for val in set(x[1] for x in inputVals):   
    print val, list(set(sval for sval in inputVals if sval[1] == val))

Note that you can also use groupby:

from itertools import groupby

data = [ (1,2,3), (3,4,5), (5,4,2)]
res = groupby(sorted(data), key=lambda x: x[1])

Edited as per comment

Played around with the problem and found one more solution - however not the best one, but:

inputVals = [(1,2,3), (3,4,5), (5,4,2), (2,2,3), (7,3,1)]
for val in set(x[1] for x in inputVals):   
    print val, list(set(sval for sval in inputVals if sval[1] == val))
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文