I have some list that consist of a tuples like this
one = [(4, 'a'), (3, 'b'), (2, 'c'), (3, 'd'), (5, 'e'), (6, 'f')]
and i want to group item on list one based on that integer to create new array that has an output like this
final = [(g1, 2, ['c']), (g2, 3, ['b','d']), (g3, 4, ['a']), (g4, 5, ['e']), (g5, 6, ['f'])]
I have no idea in creating the final list. How is python doing that? Any ideas would be appreciated. Thank you.
Note: g1, g2, and so on is just some string with increment.
Since you want the output to be sorted, you can sort the original list based on the first element
>>> first = lambda x: x[0]
>>> one_sorted = sorted(one, key=first)
then you can group the elements based on the first elements with itertools.groupby
, like this
groupby(one_sorted, first)
since you want to assign numbers, in ascending order, to the groups, you can wrap it with enumerate
like this
enumerate(groupby(one_sorted, first), 1)
then you can unpack the result of enumerate
in a for
loop, like this
for index, (item, group) in enumerate(groupby(one_sorted, first), 1)
now you just have to construct the result list. You can use list comprehension to do that, like this
>>> from itertools import groupby
>>> [(index, item, [j[1] for j in group])
... for index, (item, group) in enumerate(groupby(one_sorted, first), 1)]
[(1, 2, ['c']), (2, 3, ['b', 'd']), (3, 4, ['a']), (4, 5, ['e']), (5, 6, ['f'])]
[j[1] for j in group]
actually iterates the grouped items and fetches the second item, which is the actual string.
Alternatively, you can group the elements in a dictionary, like this
>>> groups = {}
>>> for number, string in one:
... groups.setdefault(number, []).append(string)
...
...
>>> groups
{2: ['c'], 3: ['b', 'd'], 4: ['a'], 5: ['e'], 6: ['f']}
and then return apply the enumerate
on the sorted dictionary, like this
>>> [(index, number, groups[number])
... for index, number in enumerate(sorted(groups), 1)]
[(1, 2, ['c']), (2, 3, ['b', 'd']), (3, 4, ['a']), (4, 5, ['e']), (5, 6, ['f'])]
You can use a default dict to group the items:
from collections import defaultdict
# create dictionary # {2: ['c'], 3: ['b', 'd'], 4: ['a'], ...}
groups = defaultdict(list)
for k,v in one:
groups[k].append(v)
# create sorted list [(1, (2, ['c'])), (2, (3, ['b', 'd'])), (3, (4, ['a'])), ...]
lists = enumerate(sorted(list(groups.items())), 1)
Note: The above produces an enumerator, which is generator-like object (see here). If you want to convert to a normal list just replace the last line with:
lists = [(i,k,v) for i,(k,v) in enumerate(sorted(list(groups.items())), 1)]
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With