I want to add values into multiple lists from a single list with list comprehension. There are 4 values in the feature list, which should go into 4 separate lists. my code:
features = features_time_domain(values, column_name)
feature_lists =[mean_list, std_list, max_list, min_list]
[x.append(y) for x,y in zip(feature_lists, features)]
The expected output should be something like this:
'x-axis_mean': [[0.010896254499999957,-0.01702540899999986 ,0.24993333400000006 ,-0.2805791479999999, -0.7675066368], 'x-axis_std': [4.100886585107956,3.8269951001730607, 4.19064980513631, 3.7522815775487643, 3.5302154102620498], 'x-axis_max': [6.2789803,7.668256, 11.604536, 9.384419, 7.5865335], 'x-axis_min': [-8.430995,-8.662541, -7.8861814,7.6546354,-5.175732 ]
but I get:
'x-axis_mean': [[0.010896254499999957, 4.100886585107956, 6.2789803, -8.430995], [-0.01702540899999986, 3.8269951001730607, 7.668256, -8.662541], [0.24993333400000006, 4.19064980513631, 11.604536, -7.8861814], [-0.7675066368, 3.7522815775487643, 9.384419, -7.6546354], [-0.2805791479999999, 3.5302154102620498, 7.5865335, -5.175732]], 'x-axis_std': [], 'x-axis_max': [], 'x-axis_min': []
I have looked at other post and tried to do something similar as them but my answer is off. I could do something like this, but it looks very ugly:
mean_list.append[features[0]]
std_list.append[features[1]]
max_list.append[features[2]]
min_list.append[features[3]]
CodePudding user response:
Apparently this works:
mean_list.append[features[0]]
std_list.append[features[1]]
max_list.append[features[2]]
min_list.append[features[3]]
This is simple, declarative, and clear, although this would be even clearer:
mean, std, mx, mn = features
mean_list.append(mean)
max_list.append(mx)
std_list.append(std)
min_list.append(mn)
However if your data is in a dict, something like this would be clear:
results = dict(mean=[], std=[], mx=[], mn=[])
for features in get_features():
for i, k in enumerate(results):
results[k].append(features[i])