How to count number of unique lists within list?

后端 未结 4 479
心在旅途
心在旅途 2021-01-27 12:10

I\'ve tried using Counter and itertools, but since a list is unhasable, they don\'t work.

My data looks like this: [ [1,2,3], [2,3,4], [1,2,3] ]

I would like to

4条回答
  •  再見小時候
    2021-01-27 12:20

    list =  [ [1,2,3], [2,3,4], [1,2,3] ]
    repeats = []
    unique = 0
    for i in list:
        count = 0;
        if i not in repeats:
            for i2 in list:
                if i == i2:
                    count += 1
        if count > 1:
            repeats.append(i)
        elif count == 1:
            unique += 1
    
    print "Repeated Items"
    for r in repeats:
        print r,
    
    print "\nUnique items:", unique
    

    loops through the list to find repeated sequences, while skipping items if they have already been detected as repeats, and adds them into the repeats list, while counting the number of unique lists.

提交回复
热议问题