Reputation: 3
I have a mixed list with strings and ints and I need to get to the sum of the numbers between each strings. Ideally the end result would be a list of tuples since each string and following numbers belong together (so order is important).
I can extract the number with iteration using isinstance
but the actual list is very large and I sometimes I have 1 or 2 numbers for each string.
my_list = ['a', 2, 1, 'b', 3, 'h', 50, 4, 'd', 4, 'v', 20, 7]
ideal_output = [('a', 3) ('b', 3), ('h', 54), ('d', 4), (v, 27)]
Upvotes: 0
Views: 68
Reputation: 92854
With functools.reduce function:
from functools import reduce
def accum(prev, curr): # prev - previous item, curr - current item
if prev == [] or isinstance(curr, str):
prev.append((curr, 0))
elif isinstance(curr, int):
prev[-1] = (prev[-1][0], prev[-1][1] + curr)
return prev
my_list = ['a', 2, 1, 'b', 3, 'h', 50, 4, 'd', 4, 'v', 20, 7]
res = reduce(accum, my_list, []) # [] is initializer
print(res)
The output:
[('a', 3), ('b', 3), ('h', 54), ('d', 4), ('v', 27)]
Upvotes: 0
Reputation: 21275
Here's a solution using itertools.groupby:
my_list = ['a', 2, 1, 'b', 3, 'h', 50, 4, 'd', 4, 'v', 20, 7]
from itertools import groupby
groups = groupby(my_list, key=type) # group-by the type of the value
result = []
for key, group in groups:
string = next(group) # get the string first, we'll skip over it otherwise
if key is str:
_, values = next(groups) # move the generator forward to get to the numbers
result.append((string, sum(values))) # sum up the numbers
print(result)
Output:
[('a', 3), ('b', 3), ('h', 54), ('d', 4), ('v', 27)]
It does assume that there will be at least one number between the strings. If not you can check on the len
of g
and if that's more than 1, add a 0 for the value of the first value in g
Upvotes: 2
Reputation: 7887
You can just use simple iteration as well without groupby
, this will be slightly faster because it only does one pass:
my_list = ['a', 2, 1, 'b', 3, 'h', 50, 4, 'd', 4, 'v', 20, 7]
new_list = []
new_element = []
for element in my_list:
if isinstance(element, str):
if new_element:
new_list.append(tuple(new_element))
new_element = [element, 0]
else:
new_element[1] += element
if new_element:
new_list.append(new_element)
print(new_list)
Output:
[('a', 3), ('b', 3), ('h', 54), ('d', 4), ('v', 27)]
Upvotes: 0