0

Why isn't it appending all the lists?

test = {'file1':{'subfile1':[1,2,3],'subfile2':[10,11,12]},'file5':{'subfile1':[4,678,6]},'file2':{'subfile1':[4,78,6]},'file3':{'subfile1':[7,8,9]}}
testarray = np.array([50,60,70])
for file in test.keys():
    print(test[file]['subfile1'])
    subfile1 = np.append(testarray, test[file]['subfile1'])
print(subfile1)
1
  • 1
    What is it doing? Don't just show the code. Show the results and explain what is wrong. Commented Dec 4, 2016 at 18:22

2 Answers 2

0

numpy.append returns a new NumPy array, while your code shows that you think it's adding new values to testarray. The array is not appended in-place, a new array has to be created and filled with data, thus copying both testarray and test[file]['subfile1'].

Also, note that there's no need to loop over keys and extract a value from the dictionary by one of these keys. You can loop over the items the array contains, including both keys and values:

for key, value in test.items():
    print(value['subfile1'])
    ...
Sign up to request clarification or add additional context in comments.

Comments

0

Rather than repeatedly concatenating lists to an array, collect the values in a list, and build the array just once. It is faster, and less prone to errors:

In [514]: test
Out[514]: 
{'file1': {'subfile1': [1, 2, 3], 'subfile2': [10, 11, 12]},
 'file2': {'subfile1': [4, 78, 6]},
 'file3': {'subfile1': [7, 8, 9]},
 'file5': {'subfile1': [4, 678, 6]}}
In [515]: data=[test[f]['subfile1'] for f in test]
In [516]: data
Out[516]: [[1, 2, 3], [4, 78, 6], [7, 8, 9], [4, 678, 6]]
In [517]: np.array(data)
Out[517]: 
array([[  1,   2,   3],
       [  4,  78,   6],
       [  7,   8,   9],
       [  4, 678,   6]])

If you must, build the list iteratively:

In [518]: data=[]
In [519]: for f in test.keys():
     ...:     data.append(test[f]['subfile1'])

You could concatenate at each step:

In [521]: testarray=np.array([50,60,70])
In [522]: for file in test.keys():
     ...:     testarray = np.concatenate((testarray, test[file]['subfile1']))
     ...:     
In [523]: testarray
Out[523]: 
array([ 50,  60,  70,   1,   2,   3,   4,  78,   6,   7,   8,   9,   4,  678,   6])

Notice this puts all values in one 1d array, as opposed to a 2d array that the previous methods did. We can vstack to go 2d (it too uses concatenate).

In [525]: testarray=np.array([50,60,70])
In [526]: for file in test.keys():
     ...:     testarray = np.vstack((testarray, test[file]['subfile1']))
     ...:     
     ...:     
In [527]: testarray
Out[527]: 
array([[ 50,  60,  70],
       [  1,   2,   3],
       [  4,  78,   6],
       [  7,   8,   9],
       [  4, 678,   6]])

I could also write this with append, but I'd rather not. Too many posters misuse it.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.