简体   繁体   English

如何确定嵌套列表中的所有元素是否唯一?

[英]How to determine if all elements in a nested list are unique?

I'm a beginner here and could really use some help.我是这里的初学者,真的可以使用一些帮助。

I have to determine whether all elements in a two-dimensional list is unique.我必须确定二维列表中的所有元素是否都是唯一的。 For example, if given a two-dimensional list my_list = [[1,2,2],[4,5,2],[7,2,9]] , I have to write a code that would say, "this list does not have all unique elements" because of the multiple 2s.例如,如果给定一个二维列表my_list = [[1,2,2],[4,5,2],[7,2,9]] ,我必须写一个代码,“这个list 没有所有唯一元素”,因为有多个 2。 I have to write code using nested loops.我必须使用嵌套循环编写代码。

Here is what I have so far:这是我到目前为止所拥有的:

my_list = [[1,2,2],[4,5,2],[7,2,9]]             
for row in my_list:
    for num in row:    
        if row.count(num) > 1:
            print("Duplicate")
        else:
            print("No duplicate", num)

This code can detect the duplicate 2 in the first list of my_list, but not the second list.此代码可以检测到 my_list 的第一个列表中的重复 2,但不能检测到第二个列表中的重复项。

You need to flatten the list of list and find for duplicates.您需要展平列表列表并查找重复项。 You can flatten a list of lists using itertools.chain.from_iterable您可以使用itertools.chain.from_iterable展平列表列表

from itertools import chain
my_list = [[1,2,2],[4,5,2],[7,2,9]] 
flat=list(chain.from_iterable(my_list)
if len(flat)==len(set(flat)):
    print('No dups')
else:
    print('Dups found')

Edit: Using for-loops without flattening编辑:使用 for 循环而不展平

count={}
dups=False
for lst in my_list:
    for k in lst:
        count[k]=count.setdefault(k,0)+1
        if count[k]>1:
            dups=True
            break
    if dups:
        print("dups found")
        break
else:
    print('No dups')

To do it without flattening the list of lists first, you can use a set that keeps track of items that has been "seen", so that you can determine that there is a duplicate as soon as the current item in the iteration is already in the set:要在不先展平列表列表的情况下执行此操作,您可以使用一个集合来跟踪已“看到”的项目,以便您可以在迭代中的当前项目已经存在时确定存在重复项集合:

seen = set()
for sublist in my_list:
    for item in sublist:
        if item in seen:
            print('Duplicate')
            break
        seen.add(item)
    else:
        continue
    break
else:
    print('No duplicate')

If you need a function to check that two dimensional array/list has duplicates with only nested loops:如果您需要一个函数来检查二维数组/列表是否仅具有嵌套循环的重复项:

def two_dim_list_has_duplicates(my_list):
    unique = set()
    for item in my_list:
        for i in item:
            if i in unique:
                return True
            seen.add(item)
    return False

First, collect all the elements in a one list:首先,收集一个列表中的所有元素:

all_elements = [y for x in liste for y in x]

To check whether all elements are unique:要检查所有元素是否唯一:

len(all_elements) == len(set(all_elements))

For a list of non-unique elements:对于非唯一元素列表:

list(set([x for x in all_elements if all_elements.count(x)!=1]))

But if you insist on using nested loops, you still need to keep a unique list for this check.但是如果你坚持使用嵌套循环,你仍然需要为这个检查保留一个唯一的列表。 Example:例子:

my_list = [[1,2,2],[4,5,2],[7,2,9]]
uniques = []
for row in my_list:
    for num in row:    
        if num in uniques:
            print("Duplicate",num)
        else:
            print("No duplicate", num)
        uniques.append(num)

Output:输出:

No duplicate 1
No duplicate 2
Duplicate 2
No duplicate 4
No duplicate 5
Duplicate 2
No duplicate 7
Duplicate 2
No duplicate 9

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM