[英]Appending multiple returned values to different lists in Python
I have function that calculates and returns 7 floats, and each value gets appended to a different list. 我有计算并返回7个浮点数的函数,每个值都被附加到不同的列表中。 I feel there must be something wrong if I am typing out 7 different
append
statements, but is there a more pythonic way todo this. 如果我输入7个不同的
append
语句,我觉得肯定有问题,但是有更多的pythonic方法。
For example (I don't even like typing it here!): 例如(我甚至不喜欢在这里输入它!):
a,b,c,d,e,f,g = range(7)
alist.append(a)
blist.append(b)
clist.append(c)
dlist.append(d)
elist.append(e)
flist.append(f)
glist.append(g)
According to this related question it would seem this is not possible... There must be a way using +=[a]
or list comprehension or a loop or something? 根据这个相关问题 ,似乎这是不可能的......必须有一种方法使用
+=[a]
或列表理解或循环或其他东西?
One thing that comes to mind is to keep appending all those 7 values to a master list and then transpose it at the end with zip(*MasterList)
to generate the individual lists? 我想到的一件事是继续将所有这7个值附加到主列表中,然后在最后用
zip(*MasterList)
转置它以生成单个列表?
What would people recommend? 人们会推荐什么?
appenders = range(7)
mylists = [alist, blist, clist, dlist, elist, flist, glist]
for x, lst in zip(appenders, mylists):
lst.append(x)
Well one option is to create a list of arrays, for example, something like this: 一个选项是创建一个数组列表,例如,如下所示:
# define function like this:
def range(7):
...
return [a,b,c,d,e,f,g]
# append to list:
arr = range(7)
arrList.append(arr)
# How to call your variables:
arrList[0][0] # Should return first a.
You could make this option easier to work with, using dataframes from pandas , see this introduction , where you have to import pandas as pd
( as pd
is recomended) and could define headers to your list: 您可以使用来自pandas的数据框来更容易地使用此选项, 请参阅此简介 ,您必须
import pandas as pd
(推荐使用as pd
)并且可以为列表定义标头:
data = arrList # You can use data = []
columns = ['a', 'b', 'c', 'd', 'e', 'f', 'g']
# Create the DataFrame
df = DataFrame(data, columns)
# And to add rows use*:
df.append(range(7))
*See Documentation including DataFrame.Append() *请参阅包含DataFrame.Append()的文档
Usage of this is the following: 使用方法如下:
# Usage example:
df['a'] # returns the alist
df.loc[0,'a'] # returns first value in alist
df.loc[0,:] # returns row of values of all lists
And for more usage info, see this 有关更多用法信息, 请参阅此信息
A different option is creating a function that does all those appends in one bu you'll type the code anyway, like this: 一个不同的选择是创建一个函数,无论如何都要在所有那些附加的函数中添加代码,如下所示:
def range(n):
...
return a,b,c,d,e,f,g
def appendToLists(a,b,c,d,e,f,g):
alist.append(a)
blist.append(b)
clist.append(c)
dlist.append(d)
elist.append(e)
flist.append(f)
glist.append(g)
# Example:
appendToLists(range(7))
Don't know if it's what your looking for because the list would have to be global. 不知道这是不是你想要的,因为列表必须是全局的。
Maybe you want a list of lists: 也许你想要一个列表列表:
>>> [[item] for item in range(7)]
[[0], [1], [2], [3], [4], [5], [6]]
You could iterate over them without retyping the names. 您可以在不重新输入名称的情况下迭代它们。
I like @Ryne's answer because it is straightforward, but here is the option I am pondering. 我喜欢@ Ryne的回答,因为它很简单,但这是我正在思考的选项。
# inside the loop
master_list.append(range(7))
# once you are done, transpose...
alist,blist,clist,dlist,elist,flist,glist = zip(*master_list)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.