Remove sublist duplicates including reversed

后端 未结 7 1625
眼角桃花
眼角桃花 2020-12-07 04:27

For example i have following

list = [[\'1\', \'2\'], [\'1\', \'3\'], [\'1\', \'4\'], [\'1\', \'5\'], [\'2\', \'1\'], [\'4\', \'1\'], [\'2\', \'6\']]


        
相关标签:
7条回答
  • 2020-12-07 05:02

    Approach1:

    new_list = []
    for l in List:
        if l not in new_list and sorted(l) not in new_list:
            new_list.append(l)
    
    print(new_list)
    

    Approach2:

    You can try like this also:

    seen = set()
    print([x for x in List if frozenset(x) not in seen and not seen.add(frozenset(x))])
    

    [['1', '2'], ['1', '3'], ['1', '4'], ['1', '5'], ['2', '6']]
    
    0 讨论(0)
提交回复
热议问题