this is an exercise in python, I can not use pandas just built-in python functions. I have this list:
['a', 1],
['b', 1],
['a', 2],
['a', 2],
The output, should look like:
['a', 5],
['b', 1],
This is the code I have so far, but I'm getting basically same list.
result = []
for x,y in list_data:
if x not in result:
result.append([x, int(y)])
else:
result[1] = int(y)
result
CodePudding user response:
You can make a dictionary to compute the totals and then convert it back to a list:
data = [['c', 2], ['a', 1], ['b', 1], ['a', 2], ['a', 2], ['c', 3]]
totals = {} # could use collections.defaultdict for this part
for k, v in data:
totals[k] = totals.get(k, 0) v
print(totals) # {'c': 5, 'a': 5, 'b': 1}
output = [[k, v] for k, v in totals.items()]
# or, output = list(map(list, totals.items()))
# or, output = list(totals.items()) # if you are fine with a list of tuples
print(output) # [['c', 5], ['a', 5], ['b', 1]]
# if you want the output to be sorted alphabetically
output = sorted(output, key=lambda lst: lst[0])
print(output) # [['a', 5], ['b', 1], ['c', 5]]
CodePudding user response:
One-liner:
import itertools
[[x[0], sum([value[1] for value in x[1]])] for x in itertools.groupby(sorted(list_data), lambda i: i[0])]
Details:
[[x[0], sum([value[1] for value in x[1]])] for x in itertools.groupby(sorted(list_data), lambda i: i[0])]
sorted(list_data) # sort the list alphabetically
itertools.groupby(sorted(list_data), lambda i: i[0]) # group by the "key"
# result: [('a', <itertools._grouper object at 0x000001D6B806F700>), ('b', <itertools._grouper object at 0x000001D6B806F310>)]
x[0] for x in # ['a', 'b']
[value[1] for value in x[1]] for x in # [[1, 2, 2], [1]]
sum([value[1] for value in x[1]]) for x in # [5, 1]
x[0], sum([value[1] for value in x[1]]) for x in # [['a', 5], ['b', 1]]
CodePudding user response:
Dictionary is best practice for your task.
data = [['a', 1], ['b', 1], ['a', 2], ['a', 2]]
d = {}
for key, value in data:
d[key] = value (d[key] if key in d else 0)
# generate list of tuples and sort
out = sorted(d.items())
print(out)
CodePudding user response:
It seems you are thinking of lists as a dictionary. For your code to work you need to find the result index that you want to add the value (result[1]).
But if you change the result to a dictionary you can keep almost the same code:
result = dict() # or result = {}
for x,y in list_data:
if x not in result:
result[x] = int(y)
else:
result[x] = y
print(result)
Out[1]: {'a': 5, 'b': 1}
If you want to have a list, you can use:
result_list = []
for x,y in result.items():
result_list.append([x,y])
print(result_list)
[['a', 5], ['b', 1]]