Home > Software design >  How to get a unique pattern from a list
How to get a unique pattern from a list

Time:10-21

I have a list like this:

 [    '0D',
      '0A,0C',
      '0C,0A',
      '0C,0E,0D,0F',
      '0C,0D,0E,0F',
      '0B,0G',
      '0B,0F'
]

In this list '0A,0C' and '0C,0A'.Also '0C,0E,0D,0F' & '0C,0D,0E,0F' are similar. How to get the unique items from a list like this. I tried set but I guess the functionality of set is a bit different.

CodePudding user response:

´set´ is good, if you use ´split´ first:

l = ['0D', '0A,0C', '0C,0A', '0C,0E,0D,0F', '0C,0D,0E,0F', '0B,0G', '0B,0F']

for i in range(len(l)):
    l[i] = ','.join(sorted(l[i].split(',')))

l = set(l)
# {'0A,0C', '0B,0F', '0B,0G', '0C,0D,0E,0F', '0D'} 
  • Related