I have a list looking like that:
[[12, 0.029], [12, 0.039], [12, 0.012], ...some hundreds more... [13, 0.04], [13, 0.01], ...]
The first values range from 3 to 15, with an overall count of about 3000 values
For boxplotting I would need a solution where a boxplot is created for each first value with all second values. Like:
data_to_plot = [ all second values of list with value 12], [all second values of list with value 13],...
Looking like:
data_to_plot = [0.029, 0.039], [0.04, 0.01],...
Thanks!
It seems like you want a dictionary with the first values as keys and the second values as elements of a list. You could do something like:
data_dict = {}
for key, value in list:
if key not in data_dict:
data_dict.update({key: [value]})
else:
data_dict[key].append(value)
This will yield (using your example data) {12: [0.029, 0.039], 13: [0.04, 0.01]}
Use a defaultdict with default as list so you don't need to check if a key already exists. Then collect values using the first items as keys:
from collections import defaultdict
result = defaultdict(list)
lst = [[12, 0.029], [12, 0.039], [13, 0.04], [13, 0.01]]
for l in lst:
result[l[0]].append(l[1])
print(list(result.values()))
# [[0.029, 0.039], [0.04, 0.01]]
In this way, you still know which values belong to which keys.
data_to_plot = result.values()
keys_for_data = result.keys()
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