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

2024-04-26 14:10:47 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在努力使我的一个功能发挥作用。这是它的基础:

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

但是,当我这样做时,它似乎没有将a附加到结果列表中。说到Python我是新手,所以请帮忙!


Tags: ofthe功能fordataetccodebody
3条回答

你应该加上例子。

可能您在a=final_body_code中有问题,结果是None

不过,使用list comprehension对@Mrinal Wahal answer进行了一些改进:

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

我想这是你愿意做的。

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

相关问题 更多 >