I am looking to transform a list into smaller lists of equal values. An example I have is:
["a", "a", "a", "b", "b", "c", "c", "c", "c"]
to
[["a", "a", "a"], ["b", "b"], ["c", "c", "c", "c"]]
What do you think is the most efficient way to do this?
You could use itertools.groupby to solve the problem:
>>> from itertools import groupby
>>> [list(grp) for k, grp in groupby(["a", "a", "a", "b", "b", "c", "c", "c", "c"])]
[['a', 'a', 'a'], ['b', 'b'], ['c', 'c', 'c', 'c']]
It only groups consecutive equal elements but that seems enough in your case.
You could use collections.Counter
>>> lst = ["a", "a", "a", "b", "b", "c", "c", "c", "c"]
>>> import collections
>>> collections.Counter(lst).most_common()
[('c', 4), ('a', 3), ('b', 2)]
This works even when the values are not ordered and provides a very compact representation which then you could expand if needed into lists:
>>> [[i]*n for i,n in collections.Counter(lst).most_common()]
[['c', 'c', 'c', 'c'], ['a', 'a', 'a'], ['b', 'b']]
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