简体   繁体   中英

Python Iterate through a nested list but only the even indexes

I have a nested list like this eg: L = [[A,200][B,300][C,500][A,300]] I would like to know if just the letters are duplicates not the numbers. I tried this but this would also tell me if the numbers were duplicates.

def find_duplicates(L):
    for list in L:
        for number in list:
            print(max(Counter(sum(L, [])).values()) > 1)

I didnt know whether to flatten the list and then just iterate through a single list (but this would defeat the purpose of the nested list) or if there´sa way to check for the first index of each sublist. Can anyone let me know how can I do that?

Thank you!

You can use a set to keep track of the letters that have been "seen" while iterating through the sub-lists:

def has_duplicates(L):
    seen = set()
    for letter, _ in L:
        if letter in seen:
            return True
        seen.add(letter)
    return False

I suggest using pandas,convert your list to a dataframe it will allow you to specifie which column you work with and allows you to define several operations on your list.

import pandas as pd

L = [['A',200],['B',300],['C',500],['A',300]]
df = pd.DataFrame(L,columns=['letters','numbers'])

has_dup = df['letters'].duplicated().any()

if has_dup : 
  print("L has duplicate letters")

You can use a set or a dict to keep a record of elements, and check if an element has already been found earlier:

L = [['A',200], ['B',300], ['C',500], ['A',300]]
existing_set = dict()
for letter, number in L:
    if letter in existing_set:
        print(letter, 'is a duplicate')
        existing_set[letter].append(letter)
    else:
        existing_set[letter] =[letter]

Notice that a list of numbers has also been assigned to each key so you can keep track the numbers associated to a letter.

You can use Counter directly to find the duplications:

L = [["A",200],["B",300],["C",500],["A",300]]

from collections import Counter
letterCounts     = Counter(c for c,_ in L)
duplicateLetters = list(letterCounts-Counter(letterCounts.keys()))

print(duplicateLetters) # ['A']

If you only need to know if there are any duplicate, you can use a set:

def anyDuplicates(L):
    return len(L) != len(set(c for c,_ in L))

You can access elements of a list directly by index. If L = [A,200], L[0] = 'A'.

'def find_duplicates(L):
    duplicates = []
    for i in range(len(L)):
        #fix a letter to compare
        candidate_letter = L[i]
        for j in range(len(L)):
            if i == j:
                continue
            elif candidate_letter == L[j]:
                duplicates.append(L[j])`

Of course, this can be optimized to check only half of the indices.

You can compare the items to a set of the items to see if there are repeats. This won't return the letters that are repeated though.

L = [["A", 200], ["B", 300], ["C", 500], ["D", 300]]  # lists need to have commas in between items


def has_duplicates(sequence):
    first_j = [i[0] for i in sequence]  # creates a list of only the first indexes
    if list(set(first_j)) == first_j:  # set removes duplicates
        return False
    else:
        return True


print(has_duplicates(L))

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