Python 如何将列表的每个元素分配给一个单独的变量?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19300174/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to assign each element of a list to a separate variable?
提问by David Yang
if I have a list, say:
如果我有清单,请说:
ll = ['xx','yy','zz']
and I want to assign each element of this list to a separate variable:
我想将此列表的每个元素分配给一个单独的变量:
var1 = xx
var2 = yy
var3 = zz
without knowing how long the list is, how would I do this? I have tried:
在不知道列表有多长的情况下,我该怎么做?我试过了:
max = len(ll)
count = 0
for ii in ll:
varcount = ii
count += 1
if count == max:
break
I know that varcount is not a valid way to create a dynamic variable, but what I'm trying to do is create var0
, var1
, var2
, var3
etc based on what the count is.
我知道,varcount不是要建立一个动态变量的一种有效方式,但我想要做的是创造var0
,var1
,var2
,var3
等基于计数是什么。
Edit::
编辑::
Never mind, I should start a new question.
没关系,我应该开始一个新的问题。
回答by Darrick Herwehe
You should go back and rethink why you "need" dynamic variables. Chances are, you can create the same functionality with looping through the list, or slicing it into chunks.
您应该回过头来重新思考为什么“需要”动态变量。很有可能,您可以通过循环遍历列表或将其切成块来创建相同的功能。
回答by alexis
Not a good idea to do this; what will you do with the variables after you define them?
这样做不是一个好主意;定义变量后,您将如何处理它们?
But supposing you have a good reason, here's how to do it in python:
但是假设你有一个很好的理由,下面是如何在 python 中做到这一点:
for n, val in enumerate(ll):
globals()["var%d"%n] = val
print var2 # etc.
Here, globals()
is the local namespace presented as a dictionary. Numbering starts at zero, like the array indexes, but you can tell enumerate()
to start from 1 instead.
这里,globals()
是作为字典呈现的本地命名空间。编号从 0 开始,就像数组索引一样,但您可以enumerate()
改为从 1 开始。
But again: It's unlikely that this is actually useful to you.
但同样:这不太可能对您有用。
回答by dansalmo
Instead, do this:
相反,请执行以下操作:
>>> var = ['xx','yy','zz']
>>> var[0]
'xx'
>>> var[1]
'yy'
>>> var[2]
'zz'
回答by user1767754
If the number of Items doesn't change you can convert the list to a string and split it to variables.
如果项目的数量没有改变,您可以将列表转换为字符串并将其拆分为变量。
wedges = ["Panther", "Ali", 0, 360]
a,b,c,d = str(wedges).split()
print a,b,c,d
回答by cmantas
generally speaking it is notsuggested to use that kind of programming for a large number of list elements / variables.
一般来说,不建议对大量列表元素/变量使用这种编程。
However the following statement works fine and as expected
但是,以下语句按预期工作正常
a,b,c = [1,2,3]
This is called "destructuring".
这被称为“解构”。
it could save you some lines of code in some cases, e.g. I have a,b,c as integers and want their string values as sa,sb,sc:
在某些情况下,它可以为您节省一些代码行,例如,我将 a、b、c 作为整数并希望它们的字符串值为 sa、sb、sc:
sa, sb,sc = [str(e) for e in [a,b,c]]
or, even better
或者,甚至更好
sa, sb,sc = map(str, (a,b,c) )
回答by BrianB
I am assuming you are obtaining the list by way of an SQL query - why you can't commit to a length. With that it seems obvious to me that you wish to take those values and execute additional SQL commands using each value from that newly acquired list separately. This is not a way off concept if you take it from an SQL perspective, and if I am correct I will provide the syntax. However you will not need to create separate variable names with this solution and not loose the versatility you are working toward. IF I AM WRONG SORRY FOR WASTING YOUR TIME
我假设您是通过 SQL 查询获取列表 - 为什么您不能提交长度。对我来说,很明显您希望获取这些值并分别使用新获取的列表中的每个值执行其他 SQL 命令。如果您从 SQL 的角度来看,这不是一个偏离概念的方法,如果我是正确的,我将提供语法。但是,您无需使用此解决方案创建单独的变量名称,也不会失去您正在努力实现的多功能性。如果我错了对不起浪费你的时间
回答by remanuel
I have found a decent application for it. I have a bunch of csv files which I want to save as a dataframe under their name:
我找到了一个不错的应用程序。我有一堆 csv 文件,我想以它们的名字保存为数据框:
all_files = glob.glob(path + "/*.csv")
name_list = []
for f in all_files:
name_list.append(f[9:-4])
for i,n in enumerate(name_list):
globals()[n] = pd.read_csv(all_files[i])