简体   繁体   中英

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. 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.

You need to flatten the list of list and find for duplicates. You can flatten a list of lists using 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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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