简体   繁体   中英

How to append list of all results from a For loop?

I'm struggling to get a function of mine to work. Here's the basis of what it is:

#for example:
data=[(b),(c),(d),(e)]

results=[]
for x in data: #this goes through each row of the data
    # body_code executes. This part is mostly just changing types, etc
    a=final_body_code
    results.append(a)
print results

#output should be: results=[(b),(c),(d),(e)] #After changing types of b,c,d,e etc.

#The body of the code does not matter at this point, it's just the appending which i'm
#struggling with. 

However, when I do this it does not seem to append a to the results list. I'm a newbie when it comes to python, so please help!

You should add examples.

Maybe you have some problem in a=final_body_code which a result is None

However, a little improvement to @Mrinal Wahal answer, using list comprehension :

results = [final_body_code(i) for i in data]

I Think This Is What You're Willing To Do.

data=[(b),(c),(d),(e)]

results=[]

for x in data:
    results.append(x)

print results
data = [b, c, d, e]

results = []

results.extend(final_body_code(i) for i in data)

return results

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