简体   繁体   中英

Printing out a list from a For-Loop

I the following code:

z= list(country)
del z[99]

for i in z:
    br=[]
    br=(data[i]['born'])
    print(br)

The output I get from this is:
4.5
3.2
9.0
4.3

But I want the For loop to give me a list that looks something like: [4.5, 3.2, 9.0, 4.3]

In the for loop I tried doing br=list(data[i]['born']) , but this gave an error that said 'numpy.float32' object is not iterable How can I fix this issue?

Meaby you can try to use the append method to add each element of the first list into the new empty list that is br=[], Try this in the for loop: ´´´ br.append(i) ´´´

Two possible ways:

br = []
for i in z:
    br.append(data[i]['born'])
print(br)

or (more concise):

print(list(data[i]['born'] for i in x))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM