Below is my working code:
import numpy as np
test = [{"test":np.array([["value1,value1"],["value2,value2"]])},{"names":
["test1","test2"]}]
q = ["value3,value3"]
v = ["test3"]
for p in test:
for item,value in p.items():
if str(item).startswith("test"):
p["test"] = np.append(p["test"],np.array([q]))
if str(item).startswith("names"):
for r in v:
p["names"].append(r)
I need output as following:
[{'test': array([['value1,value1'], ['value2,value2'], ['value3', 'value3']],
dtype='<U13')},
{'names': ['test1', 'test2', 'test3']}]
but currently im getting like below:
[{'test': array(['value1,value1', 'value2,value2', 'value3', 'value3'], dtype='<U13')},
{'names': ['test1', 'test2', 'test3']}]
Where am I doing silly mistake ? Thank you.
CodePudding user response:
Your need to append to a specific axis:
for p in test:
for item,value in p.items():
if str(item).startswith("test"):
p["test"] = np.append(p['test'], [q], axis=0) # changed here
if str(item).startswith("names"):
for r in v:
p["names"].append(r)
output:
[{'test': array([['value1,value1'],
['value2,value2'],
['value3,value3']], dtype='<U13')},
{'names': ['test1', 'test2', 'test3']}]