Reputation: 25
I'm relatively new with Python and am having a hard time coming up with a way to make the following work. Currently, I have a list of tuples (larger than list_one
. The elements inside these tuples are 1) a tuple, and 2) a string (these strings are always one of two values: in the example 'A'
or 'B'
(can also be 'D'
or 'Z'
, or any other two strings). The inner tuples include an 'id'
and a list
of values (strings):
list_one = [(('id1', ['v1', 'v2']), 'A'), (('id2', ['v3', 'v4', 'v5', 'v6']), 'A'), (('id3', ['v11']), 'B'), (('id4', ['v12', 'v13']), 'B'), (('id5', ['v14', 'v16']), 'B'), (('id6', ['v17', 'v18', 'v21']), 'A')]
I'm trying, unsuccessfully, to create a new list that looks as follows:
new_list1 = [('id1',['v1', 'v2', 'v3', 'v4', 'v5', 'v6']), ('id3',['v11', 'v12', 'v13', 'v14', 'v16']), ('id6',['v17', 'v18', 'v21'])]
When the string value (A, B) changes it should create a new tuple with the first element being the id of the initial tuple where the A,B changes, and the second element being a list of the values ("v's") inside the initial lists. A list if dictionaries would also be fine, e.g:
new_list2 = [{'id1': ['v1','v2', 'v3', 'v4', 'v5', 'v6']}, {'id3':['v11', 'v12', 'v13', 'v14', 'v16']}, {'id6':['v17', 'v18', 'v21']}]
I appreciate any guidance.
edit:
s = list_one[0][1]
for tup in list_one:
if s in tup:
new_list.extend(tup[0][1])
else:
new_list.append(tup[0][1])
gives:
['v1', 'v2', 'v3', 'v4', 'v5', 'v6', ['v11'], ['v12', 'v13'], ['v14', 'v16'], 'v17', 'v18', 'v21']
which is not really what I'm looking for, as this creates a list around one the 'A' value, with lists of 'B' inside.
Upvotes: 2
Views: 340
Reputation: 307
Edit
This solves a related question, whereby all the lists corresponding to 'A'
and 'B'
values are aggregated, and the id is chose of the total first id for each group.
Here you go
letterKeyToFirstIDMap = {}
result_dict = {}
for tpl in list_one:
id = tpl[0][0]
lst = tpl[0][1]
letter_id = tpl[1]
if not letter_id in letterKeyToFirstIDMap:
letterKeyToFirstIDMap.update({letter_id: id})
result_dict.update({id:[]})
result_dict[letterKeyToFirstIDMap[letter_id]] += lst
print result_dict
What is done here is that for each letter key (say A) you want only the first id that is met (say id1) to be the key for the following list. You create a map to store the letterKey and its idKey the first time the pair appears and then just check if the letter key already exists in that map, using it's corresponding idKey if it does, and update the list.
Upvotes: 1
Reputation: 76297
You can basically loop along, remembering what is the current key (initialized to None
).
key = None # This is 'A' or 'B' in your example, but it starts off as None
new_list = [] # This holds the final result
for r, l in [(r, l) for (l, r) in list_one]:
if r != key: # If the current is not the key, time to switch to a new one
id_ = l[0]
new_list.append({id_: []})
new_list[-1][id_].extend(l[1]) # Extend the current list
key = r # Make it the current key in any case.
>>> new_list
[{'id1': ['v1', 'v2', 'v3', 'v4', 'v5', 'v6']},
{'id3': ['v11', 'v12', 'v13', 'v14', 'v16']},
{'id6': ['v17', 'v18', 'v21']}]
Upvotes: 1