简体   繁体   中英

Eliminate duplicated sublists when sublists are uneven in Python

Consider the list:

    test = [['B', [4, 5, 6]], ['C', [7, 8, 9]], ['C', [7, 8, 9]]]

I want the return to be: [['B', [4, 5, 6]], ['C', [7, 8, 9]]]

I am able to get the result using loop structures:

    new_test = []

    for sub in test:
            if sub not in new_test:
                    new_test.append(sub)    
    
    #[['B', [4, 5, 6]], ['C', [7, 8, 9]]]

I've ran some tests with the popular set-tuple method, but it doesn't work here because sublists are uneven:

    set(tuple(x) for x in test)

I've tried with the original test list and also tried:

    test = [[4, [5, 6],[7, 8], 9],[7, 8, 9]]
    

Both return same error:

Error: set(tuple(x) for x in test)

TypeError: unhashable type: 'list'

Is there any other way to get rid off duplicates without "for loops"? I have no big issues with loops, I just want to explore other types of coding in Python. The order of the sublists are not important.

If the equal sublists are consecutive as in the example, you can use itertools.groupby here:

from itertools import groupby

[next(v) for k,v in groupby(test)]
# [['B', [4, 5, 6]], ['C', [7, 8, 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