Numpy append array isnt working

2024/10/16 2:30:41

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)
Answer

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'])...
https://en.xdnf.cn/q/117763.html

Related Q&A

Select a valid choice ModelChoiceField

Whenever im running form.is_valid() i get the error: Select a valid choice. That choice is not one of the availablechoices.Here is what I do in my view:timeframes = HostTimeFrame.objects.all() if reque…

Let a module file use a global variable?

Forgive me if this is just a super easy solution as I am pretty new to Python. Right now Im trying to make a basic video game, and to save space I decided to make a module for a combat encounter -- so …

Python Subprocess readline hangs() after reading all input

I am trying to readlines from the tcp server that I ran in the same script. I am able to send one command and reads its output but at the end (after reading all outputs) it looks like that program hang…

Python Counting countries in dictionary

Im writing a function that counts the number of times a country appears in a dictionary and returns the country that appeared the most. If more then one country appears the most then it should return a…

How to wait for any socket to have data?

Im implementing a socket-client which opens several sockets at the same time. Any socket may have data at a different time and I want to execute code when any socket has data and is readable.Im not sur…

Retrieving ad URLs

Im looking for a way to retrieve the ad URLs for this website. http://www.quiltingboard.com/resources/What I want to do is probably write a script to continuously refresh the page and grab the ad URLs.…

Extract data (likes) from JSON API using Python

I want to pull the number of likes for my project. Heres my code: import facepy from facepy import GraphAPI from bs4 import BeautifulSoup import json access = CAACEdEose0cBAE3IL99IreDeAfqaVZBOje8ZCqIhf…

No nested nodes. How to get one piece of information and then to get additional info respectively?

For the code below I need to get dates and their times+hrefs+formats+...(not shown) respectively.<div class="showtimes"><h2>The Little Prince</h2><div class="poster&…

I need help changing the color of text in python

Hey I need help with coloring the text in a program I am making. It is a password program and I am trying to make the denied and granted red and green when they appear. Here is the program so far:passw…

How do I use Liclipse to write a ParaView script?

Ive tried following the directions here without success. Here are some of my environment variables:Path: C:\Python34\;C:\Python34\Scripts;...;C:\Program Files (x86)\ParaView 4.3.1\lib\paraview-4.3\site…