可变的地狱 [英] variable hell
问题描述
嗨
我正在将一个列表打包成变量,出于某种原因,他们需要将
解压缩到变量中像a0,a1,a2 ......这样的名字直到aN
列表中的内容。
如何动态创建变量?
我正在寻找类似
的伪代码行如下:
a%s = str(value)
下面是mylist解包代码的片段
#next行从列表中的每一行剪下行尾字符
mylist = [line [: - 1] for mylist中的行]
表示索引,值为枚举(mylist):
如果index == 0:
a0 = str(值)
打印" a0:",a0
elif index == 1:
a1 = str(价值)
打印" a1:",a1
谢谢
Nx
Hi
I am unpacking a list into variables, for some reason they need to be
unpacked into variable names like a0,a1,a2....upto aN whatever is
in the list.
How to create the variables dynamically ?
I am looking for something like
pseudo code line follows :
a%s = str(value)
here below is a snippet from the mylist unpack code
#next lines cut the end of line character from each line in the list
mylist = [line[:-1] for line in mylist]
for index,value in enumerate(mylist):
if index == 0 :
a0 = str(value)
print "a0 : ",a0
elif index == 1 :
a1 = str(value)
print "a1 : ",a1
Thanks
Nx
推荐答案
Nx写道:
Nx wrote:
我是unpa将列表转换为变量,由于某种原因,它们需要被解压缩为变量名,如a0,a1,a2 ....直到列表中的任何内容。
I am unpacking a list into variables, for some reason they need to be
unpacked into variable names like a0,a1,a2....upto aN whatever is
in the list.
解释这个某种原因。这有点味道,而且要走的路是
使用dict将a_n映射到列表中的任何内容 - 不创建
变量。无论如何,你想如何访问生成的变量 -
特别是当你不知道有多少人在那里?b $ b是多少?显然,基于此不能编写代码。
问候,
Diez
Explain this "some reason". This smells, and the way to go would be to
use a dict mapping a_n to whatever is in the list - not creating
variables. How do you want to access generated variables anyway -
especially when you don''t have the faintest idea how many of them there
are? Obviously there can''t be code written based on that.
Regards,
Diez
Nx启发我们:
我正在将列表拆分为变量,由于某种原因,他们需要将包解压缩为变量名,如a0,a1, a2 ......直到列表中的任何内容。
I am unpacking a list into variables, for some reason they need to
be unpacked into variable names like a0,a1,a2....upto aN whatever is
in the list.
你可能做错了事。你的最终目标是什么?b $ b这个?可能有更好的方法。
同时,看看eval()。
Sybren
-
世界的问题是愚蠢。并不是说应该对愚蠢的死刑进行处罚,但为什么我们不要仅仅拿掉
安全标签来解决问题呢? br />
Frank Zappa
You''re probably doing things the wrong way. What is your ultimate goal
with this? There is probably a better way of doing it.
In the mean time, look at eval().
Sybren
--
The problem with the world is stupidity. Not saying there should be a
capital punishment for stupidity, but why don''t we just take the
safety labels off of everything and let the problem solve itself?
Frank Zappa
Nx写道:
嗨
我正在打包列表变量,由于某种原因,它们需要被解压缩成变量名,如a0,a1,a2 ....直到列表中的任何内容。
Hi
I am unpacking a list into variables, for some reason they need to be
unpacked into variable names like a0,a1,a2....upto aN whatever is
in the list.
>
真的吗?为什么?
-
Robert Kern
rk ** *@ucsd.edu
在地狱的地方,草地长得很高
梦想的坟墓是否已经死亡。
- Richard Harter
Really? Why?
--
Robert Kern
rk***@ucsd.edu
"In the fields of hell where the grass grows high
Are the graves of dreams allowed to die."
-- Richard Harter
这篇关于可变的地狱的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!