I have a few methods which pass different amount of messy data to this function to combine headers with data and return a list of dictionaries:
def zip_data(self, indicator_names, indicator_values):values = [[float(elem) for elem in item] for item in np.nditer(indicator_values)]return [dict(zip(indicator_names, row)) for row in values]
It's basically something like (they do the same):
def zip_large_data(self, indicator_names, indicator_values):data = []for item in np.nditer(indicator_values):values = []values.append(int(item[0]))for elem in item[1:]:values.append(float(elem))data.append(dict(zip(indicator_names, values)))return data
The thing is, it works great if a list of 20 elements is passed, but for like 40 it gives the error:
File "xy.py", line 205, in momentum_indicatorsreturn self.zip_large_data(momentum_indicator_names, momentum_indicator_values)
File "xy.py", line 51, in zip_large_datafor item in np.nditer(indicator_values):
ValueError: Too many operands
How many values can np.nditer() iterate over? Is there any way to avoid this?
Edit
Small example:
indicator_values = [array([1, 2, 3, 4, 5]), array([5, 10, 15, 20,25])]
indicator_names = ['a', 'b']
Wanted output:
data = [{'a': 1, 'b': 5}, {'a': 2, 'b': 10}, {'a': 3, 'b': 15}, {'a':4, 'b': 20}, {'a': 5, 'b': 25}]
Current status:
def zip_large_data(self, indicator_names, indicator_values):data = []print(indicator_values[0])for item in np.nditer(indicator_values):print(item)values = []values.append(int(item[0]))for elem in item[1:]:values.append(float(elem))data.append(dict(zip(indicator_names, values)))print(data)breakreturn data
Output:
In: print(indicator_values[0])
Out: [1 2 3 4 5]
In: print(item)
Out:(array(1), array(5))
In: print(data)
Out: [{'a': 1, 'b': 5}]
So basically I do not want to iterate through the indicator_values sequentially, but first elements of every array, then the second elements of every array etc.. I want to avoid nditer, but don't see how
Sorry English is not my first language, first time working with numpy, it's confusing.