How to compare each item in a list with the rest, only once?

后端 未结 5 522
误落风尘
误落风尘 2020-12-07 09:02

Say I have an array/list of things I want to compare. In languages I am more familiar with, I would do something like

for (int i = 0, i < mylist.size(); i         


        
5条回答
  •  难免孤独
    2020-12-07 09:14

    This code will count frequency and remove duplicate elements:

    from collections import Counter
    
    str1='the cat sat on the hat hat'
    
    int_list=str1.split();
    
    unique_list = []
    for el in int_list:
    
        if el not in unique_list:
            unique_list.append(el)
        else:
            print "Element already in the list"
    
    print unique_list
    
    c=Counter(int_list)
    
    c.values()
    
    c.keys()
    
    print c
    

提交回复
热议问题