Keep duplicates in a list in Python

后端 未结 4 1986
悲哀的现实
悲哀的现实 2021-01-12 01:00

I know this is probably an easy answer but I can\'t figure it out. What is the best way in Python to keep the duplicates in a list:

x = [1,2,2,2,3,4,5,6,6,7         


        
4条回答
  •  太阳男子
    2021-01-12 01:06

    Not efficient but just to get the output, you could try:

    import numpy as np
    
    def check_for_repeat(check_list):
        repeated_list = []
    
        for idx in range(len(check_list)):
            elem = check_list[idx]
            check_list[idx] = None
    
            if elem in temp_list:
                repeated_list.append(elem)
    
        repeated_list = np.array(repeated_list)
    
        return list(np.unique(repeated_list))
    

提交回复
热议问题