How to get the unique pairs from the given data frame column with file handling?

后端 未结 2 1243
半阙折子戏
半阙折子戏 2021-01-24 01:06

sample data from dataframe:

Pairs

(8, 8), (8, 8), (8, 8), (8, 8), (8, 8)
(6, 7), (7, 7), (7, 7), (7, 6), (6, 7)
(2, 12), (12, 3), (3, 4), (4, 12), (12,          


        
2条回答
  •  北海茫月
    2021-01-24 01:17

    You could use the set method:

    data = (((8, 8), (8, 8), (8, 8), (8, 8), (8, 8)),
      ((6, 7), (7, 7), (7, 7), (7, 6), (6, 7)),
      ((2, 12), (12, 3), (3, 4), (4, 12), (12, 12)))
    
    uniques = []
    
    for col in data:
      for unique in list(set(col)):
        uniques.append(unique)
    
    for x in uniques:
      print(x)
    

    OR:

    data = (((8, 8), (8, 8), (8, 8), (8, 8), (8, 8)),
      ((6, 7), (7, 7), (7, 7), (7, 6), (6, 7)),
      ((2, 12), (12, 3), (3, 4), (4, 12), (12, 12)))
    
    uniques = []
    
    for col in data:
      uniques += [unique for unique in list(set(col))]
    
    for x in uniques:
      print(x)
    

提交回复
热议问题