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

后端 未结 2 1244
半阙折子戏
半阙折子戏 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:16

    You can use set() method to remove duplicates from the list of tuples.

     >>> items = [(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)]
    >>> set_items = set(items)
    >>> set_items
    {(6, 7), (7, 6), (12, 12), (7, 7), (8, 8), (4, 12), (2, 12), (3, 4), (12, 3)}
    
    0 讨论(0)
  • 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)
    
    0 讨论(0)
提交回复
热议问题