Consolidating Elements Of A List
I have a list of tuples: [('fruit', 'O'), ('is', 'O'), ('the', 'O'), ('subject', 'O'), ('of', 'O'), ('a', 'O'), ('Roald', 'PERSON'), ('Dahl', 'PERSON'), ('children', 'O'), (''s', '
Solution 1:
You can use itertools.groupby
to group utilizing the last element in each tuple:
import itertools
s = [(1, 2), (3, 4), (5, 4), (10, 4), (7, 8)]
s = [(a, list(b)) for a, b in itertools.groupby(s, key=lambda x:x[-1])]
final_s = [(sum(i[0] for i in b), a) for a, b in s]
Output:
[(1, 2), (18, 4), (7, 8)]
Edit: regarding your new, non numeric list of tuples, you can try this:
from functools import reduce
def remove(data, to_avoid='O'):
s = [(a, list(b)) for a, b in itertools.groupby(data, key=lambda x:x[-1])]
final_s = [x for i in [b if a == to_avoid else [(reduce(lambda c, d: "{} {}".format(c, d), [h[0] for h in b]), a)] for a, b in s] for x in i]
return final_s
>>remove([('fruit', 'O'), ('is', 'O'), ('the', 'O'), ('subject', 'O'), ('of', 'O'), ('a', 'O'), ('Roald', 'PERSON'), ('Dahl', 'PERSON'), ('children', 'O'), ("'s", 'O'), ('book', 'O'), ('?', 'O')])
Output:
[('fruit', 'O'), ('is', 'O'), ('the', 'O'), ('subject', 'O'), ('of', 'O'), ('a', 'O'), ('Roald Dahl', 'PERSON'), ('children', 'O'), ("'s", 'O'), ('book', 'O'), ('?', 'O')]
For those of us less comprehension aware, and using operator.itemgetter instead of the lamda's
import itertools, operator
item0 = operator.itemgetter(0)
item1 = operator.itemgetter(1)
result = []
for k, g in itertools.groupby(s, key=item1):
if k != 'O':
result.append((' '.join(map(item0, g)),k))
else:
result.extend(g)
Post a Comment for "Consolidating Elements Of A List"