如何检查嵌套列表的每个子列表中是否存在一个值?

发布于 2025-01-24 05:15:26 字数 636 浏览 0 评论 0 原文

我有一个列表,其中包含多个子列表,每个列表都充满了随机整数。我试图找出一种方法来检查每个sublist中出现的第一个sublist中的哪些元素,这是我到目前为止提出的:

            for element in sublist1:
                x = 1
                for i in range(len(list_of_sublists)-1):
                    if element in list_of_sublists[i]:
                        x = x + 1
                    elif element not in list_of_sublists[i]:
                        x = x - 1
                if x == len(allDocIDs):
                    listOfDocumentIDs.append(element)
                else:
                    pass

当有2个sublists时,它似乎可以使用,但是一旦有3个或更多似乎该代码忽略了它们的存在。是否有一种更简单的(功能更大)的方法来执行此任务?

I have a list that contains multiple sublists, each filled with random integers. I'm trying to figure out a way to check which elements from the first sublist are present in every sublist, here's what I came up with so far:

            for element in sublist1:
                x = 1
                for i in range(len(list_of_sublists)-1):
                    if element in list_of_sublists[i]:
                        x = x + 1
                    elif element not in list_of_sublists[i]:
                        x = x - 1
                if x == len(allDocIDs):
                    listOfDocumentIDs.append(element)
                else:
                    pass

It seems to work when there are 2 sublists, but once there are 3 or more it seems like the code is ignoring their existence. Is there a simpler (and more functional) way to perform this task?

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

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

发布评论

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

评论(2

温折酒 2025-01-31 05:15:26

您可以通过第一个sublist进行循环( set 只能确保for循环中的每个i在唯一的界限,以便您不检查值两次),并检查每个值是否在每个sublist中都有全部

a = [[1,2,3,4], [2,2,2,2,1], [3,3,3,3,1], [4,4,4,1,4,4]]
for i in set(a[0]):
    print(i, all((i in j for j in a)))

输出:

1 True
2 False
3 False
4 False

或与列表理解:

[(i, all((i in j for j in a))) for i in list(set(a[0]))]

输出:

[(1, True), (2, False), (3, False), (4, False)]

You can do loop through the first sublist (set just ensures that every i in the for loop is unique so you don't check a value twice) and check if each value is in every sublist with all:

a = [[1,2,3,4], [2,2,2,2,1], [3,3,3,3,1], [4,4,4,1,4,4]]
for i in set(a[0]):
    print(i, all((i in j for j in a)))

Output:

1 True
2 False
3 False
4 False

Or with list comprehension:

[(i, all((i in j for j in a))) for i in list(set(a[0]))]

Output:

[(1, True), (2, False), (3, False), (4, False)]
撩动你心 2025-01-31 05:15:26

如果该元素在第一个统一者和所有其他标准列表中,则在所有统一的人中。因此,您的问题等同于询问所有列表中的项目。您可以通过将所有标准列表的交集来有效地使用集合来做到这一点:

a = [[1,2,3,4], [2,2,4,2,1], [3,4,3,3,1], [4,4,4,1,4,4]]

set.intersection(*[set(l) for l in a])
# {1, 4}

这假设您不在乎第一个Sublist中的重复项。如果您 do 关心第一个sublist中的重复项,则可以访问其余的Sublist的交叉点,然后使用列表理解来过滤第一个元素:

a = [[1, 2, 3, 4, 1], [2,2,4,2,1, 6], [3,4,3,3,1, 6], [4,4,4,1,4,4, 6]]

elements = set.intersection(*[set(l) for l in a[1:]])
[n for n in a[0] if n in elements]
# [1, 4, 1]

If the element is in the first sublist and in all the other sublists, then it's in all sublists. So your questions is equivalent to asking which items are in all lists. You can do this efficiently with sets by taking the intersection of all the sublists:

a = [[1,2,3,4], [2,2,4,2,1], [3,4,3,3,1], [4,4,4,1,4,4]]

set.intersection(*[set(l) for l in a])
# {1, 4}

This assumes you don't care about duplicates in the first sublist. If you do care about duplicates in the first sublist, you can take the intersection of the rest of the sublist and then use a list comprehension to filter the elements from the first:

a = [[1, 2, 3, 4, 1], [2,2,4,2,1, 6], [3,4,3,3,1, 6], [4,4,4,1,4,4, 6]]

elements = set.intersection(*[set(l) for l in a[1:]])
[n for n in a[0] if n in elements]
# [1, 4, 1]
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文