Reputation: 59
I want to remove and order my list of element
['elt9', 'elt19', 'elt1', 'elt2', 'elt3','elt9','elt2']
and get
['elt1', 'elt2', 'elt3', 'elt9', 'elt19']
This is all my code:
import itertools as it
import re
from collections import OrderedDict
from itertools import chain
L1 = ['elt1', 'elt2', 'elt3', 'elt4', 'elt5', 'elt6', 'elt9']
L2 = [['elt1','elt11'],['elt2','elt12'],['elt3','elt13'], ['elt4','elt14']]
def generate_combinations(L):
L_com = []
for r in range(1, len(L)+1):
L_com += list(it.combinations(L, r))
all_combination= []
for i in L_com:
for j in L2:
all_combination.append(j+list(i))
l = sorted(set(all_combination), key = lambda x : int(re.findall(r'\d+', x)[0]))
with open('combinations.txt', 'w') as file_handler:
for item in l:
file_handler.write("{}\n".format(item))
if __name__ == "__main__":
generate_combinations(L1)
I had this error :
TypeError Traceback (most recent call last)
<ipython-input-49-e0b599cc4158> in <module>()
1 if __name__ == "__main__":
----> 2 generate_combinations(L1)
<ipython-input-45-81ef5db3553e> in generate_combinations(L1)
21
22 #sorted(set(all_combination), key=lambda x: int(x[3:]))
---> 23 l = sorted(set(all_combination), key = lambda x : int(re.findall(r'\d+', x)[0]))
24
25 with open('combinations.txt', 'w') as file_handler:
TypeError: unhashable type: 'list'
Upvotes: 1
Views: 62
Reputation: 7206
In variable all_combination you have two-dimensional list/array ([['elt1', 'elt11', 'elt1'], ['elt2', 'elt12', 'elt1'], ['elt3', 'elt13', 'elt1'], ['elt4', 'elt14', 'elt1'], ['elt1', 'elt11', 'elt2'], ...) and with that you are trying to do:
l = sorted(set(all_combination), key = lambda x : int(re.findall(r'\d+', x)[0]))
that part of the code works with the types of lists you mentioned in your post:
L1 = ['elt9', 'elt19', 'elt1', 'elt2', 'elt3','elt9','elt2']
l = sorted(set(L1), key = lambda x : int(re.findall(r'\d+', x)[0]))
print (l)
result: ['elt1', 'elt2', 'elt3', 'elt9', 'elt19']
Upvotes: 0
Reputation: 4792
You can try this:
import re
l= ['elt9', 'elt19', 'elt1', 'elt2', 'elt3','elt9','elt2']
l = sorted(set(l), key = lambda x : int(re.findall(r'\d+', x)[0]))
l
['elt1', 'elt2', 'elt3', 'elt9', 'elt19']
This will also work for any number digit number(3, 4 digit number etc) and not just 2. But the caveat is it should only have one number for it to work. The re.findall finds all the patterns provided to it and returns a list satisfying that pattern. The pattern \d+
represents one or more integers.
Upvotes: 1
Reputation: 13255
Use set
with sorted
:
l = ['elt9', 'elt19', 'elt1', 'elt2', 'elt3','elt9','elt2']
sorted(set(l), key=lambda x: int(x[3:]))
['elt1', 'elt2', 'elt3', 'elt9', 'elt19']
Upvotes: 1