如何从For循环中追加所有结果的列表?

时间:2014-08-31 05:00:24

标签: python list loops printing append

我努力让我的功能得以发挥作用。以下是它的基础:

#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. 

然而,当我这样做时,它似乎没有附加到结果列表。我是python的新手,所以请帮忙!

3 个答案:

答案 0 :(得分:2)

您应该添加示例。

也许您在a=final_body_code中遇到了一些问题,结果是None

然而,使用list comprehension

对@Mrinal Wahal的回答略有改善
results = [final_body_code(i) for i in data]

答案 1 :(得分:0)

我认为这就是你愿意做的事。

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

results=[]

for x in data:
    results.append(x)

print results

答案 2 :(得分:-1)

data = [b, c, d, e]

results = []

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

return results