迭代创建变量? [英] Iterate creating variables?

查看:101
本文介绍了迭代创建变量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要创建二十五个复选框(不要问):


self.checkbox1 = ...

self。 checkbox2 = ...

..

..

..

self.checkbox25 = ...


现在,我的代码中有25行,每个复选框一行,因为

这些都是变量。


有没有办法写一个循环,这样我可以用更少的代码行

但仍保留变量?


我''尝试过:

$ x $ b for x in xrange(25):

self.checkbox [o] = ...


没有用,而且


for x in xrange(25):

self.checkbox [''''%d' '%(o)] = ...


也没用。


两者都给出错误信息:属性错误:Main.App没有

属性复选框",这清楚地表明我没有保持

的可变性。我想要的方面。


有办法吗?


我感谢所有答案!


谢谢!

I have twenty-five checkboxes I need to create (don''t ask):

self.checkbox1 = ...
self.checkbox2 = ...
..
..
..
self.checkbox25 = ...

Right now, my code has 25 lines in it, one for each checkbox, since
these are all variables.

Is there a way to write a loop so that I can have fewer lines of code
but still keep the variables?

I''ve tried:

for o in xrange(25):
self.checkbox[o] = ...

which didn''t work, and

for o in xrange(25):
self.checkbox[''''%d''%(o)] = ...

which also didn''t work.

Both give the error message: "Attribute error: Main.App has no
attribute "checkbox"", which clearly indicates that I''m not keeping
the "variability" aspect I want.

Is there a way?

I appreciate any and all answers!

Thanks!

推荐答案

td **** @ gmail .com schrieb:
td****@gmail.com schrieb:

我需要创建二十五个复选框(不要问):


self.checkbox1 = ...

self.checkbox2 = ...







self.checkbox25 = ...


现在,我的代码中有25行,每个复选框一行,因为

这些都是变量。


有没有办法写一个循环,这样我可以减少代码行数

但仍保持变量?


我试过了:

$ x $ b for x in xrange(25):

self .checkbox [o] = ...


哪些没用,而且


for x in xrange(25):

self.checkbox ['''''%d''%(o)] = ...


也没用。


两者都给出错误信息:属性错误:Main.App没有

属性复选框",这清楚地表明我没有保持

的可变性。我想要的方面。


有办法吗?
I have twenty-five checkboxes I need to create (don''t ask):

self.checkbox1 = ...
self.checkbox2 = ...
.
.
.
self.checkbox25 = ...

Right now, my code has 25 lines in it, one for each checkbox, since
these are all variables.

Is there a way to write a loop so that I can have fewer lines of code
but still keep the variables?

I''ve tried:

for o in xrange(25):
self.checkbox[o] = ...

which didn''t work, and

for o in xrange(25):
self.checkbox[''''%d''%(o)] = ...

which also didn''t work.

Both give the error message: "Attribute error: Main.App has no
attribute "checkbox"", which clearly indicates that I''m not keeping
the "variability" aspect I want.

Is there a way?



保持列表或字典。像这样:


checkboxes = []

$ x $ b for x in xrange(25):

checkboxes.append (....创建一个复选框...)


self.checkboxes =复选框


Diez

Keep either a list or dictionary around. Like this:

checkboxes = []

for o in xrange(25):
checkboxes.append(....create a checkbox...)

self.checkboxes = checkboxes

Diez




-----原始消息-----

来自:py **** ****************************@python.org [mailto:python-

li **** *********************@python.org]代表Diez B. Roggisch

发送日期:2008年6月13日星期五11 :21 AM

致: py*********@python.org

主题:回复:迭代创建变量?

td *** *@gmail.com schrieb:
-----Original Message-----
From: py********************************@python.org [mailto:python-
li*************************@python.org] On Behalf Of Diez B. Roggisch
Sent: Friday, June 13, 2008 11:21 AM
To: py*********@python.org
Subject: Re: Iterate creating variables?

td****@gmail.com schrieb:

我需要创建二十五个复选框(不要问):


self.checkbox1 = ...

self.checkbox2 = ...







self.checkbox25 = ...


现在,我的代码中有25行,每个复选框一行,因为

这些都是变量。


有没有办法写一个循环,这样我可以有更少的行
I have twenty-five checkboxes I need to create (don''t ask):

self.checkbox1 = ...
self.checkbox2 = ...
.
.
.
self.checkbox25 = ...

Right now, my code has 25 lines in it, one for each checkbox, since
these are all variables.

Is there a way to write a loop so that I can have fewer lines of



code

code


但仍保留变量?
but still keep the variables?



保持列表或字典。像这样:


checkboxes = []

$ x $ b for x in xrange(25):

checkboxes.append (....创建一个复选框...)


self.checkboxes =复选框

Keep either a list or dictionary around. Like this:

checkboxes = []

for o in xrange(25):
checkboxes.append(....create a checkbox...)

self.checkboxes = checkboxes



如果你''太懒了回去把25个复选框转换成

数组/列表/字典,这将从现有的

变量名创建一个列表:


s1 =''one''

s2 =''两个''

s3 =''三''

s4 =''四''

s_list = []

s_list.append(无)#因为没有s0,所以''使用1个基础列表


for i in range(1,5):

code =" s%d" %i

s_list.append(eval(code))

print s_list


*****


传输的信息仅适用于其所针对的个人或实体,可能包含机密,专有和/或特权材料。禁止对除预定接收者以外的个人或实体依赖此信息进行任何审查,转播,传播或其他使用或采取任何行动。如果您错误地收到此消息,请与发件人联系并删除所有计算机中的资料。 GA621


And if you''re too lazy to go back and convert the 25 checkboxes to an
array/list/dictionary, this will create a list from the existing
variable names:

s1 = ''one''
s2 = ''two''
s3 = ''three''
s4 = ''four''

s_list = []
s_list.append(None) # since there is no s0, let''s use 1 based list

for i in range(1, 5):
code = "s%d" % i
s_list.append(eval(code))
print s_list

*****

The information transmitted is intended only for the person or entity to which it is addressed and may contain confidential, proprietary, and/or privileged material. Any review, retransmission, dissemination or other use of, or taking of any action in reliance upon this information by persons or entities other than the intended recipient is prohibited. If you received this in error, please contact the sender and delete the material from all computers. GA621




-----原始消息-----

来自:py********************************@python.org [mailto:python-

li*************************@python.org]代表 td **** @ gmail.com

发送时间:2008年6月13日星期五上午11:11

收件人:< a href =mailto:py ********* @ python.org> py ********* @ python.org

主题:迭代创建变量?


我需要创建二十五个复选框(不要问):


self.checkbox1 = ...

self.checkbox2 = ...







self.checkbox25 = ...


现在,我的代码中有25行,每个复选框一行,因为

这些都是变量。


有没有办法写一个循环,这样我可以用更少的代码行

但仍保留变量?


我没有ried:

$ x $ b for x in xrange(25):

self.checkbox [o] = ...


这是行不通的,而且


for x in xrange(25):

self.checkbox [''''%d'' %(o)] = ...


也没用。


两者都给出错误信息:属性错误:Main.App没有

属性"复选框",这清楚地表明我没有保持

变异性"我想要的方面。


有办法吗?


我感谢所有答案!
-----Original Message-----
From: py********************************@python.org [mailto:python-
li*************************@python.org] On Behalf Of td****@gmail.com
Sent: Friday, June 13, 2008 11:11 AM
To: py*********@python.org
Subject: Iterate creating variables?

I have twenty-five checkboxes I need to create (don''t ask):

self.checkbox1 = ...
self.checkbox2 = ...
.
.
.
self.checkbox25 = ...

Right now, my code has 25 lines in it, one for each checkbox, since
these are all variables.

Is there a way to write a loop so that I can have fewer lines of code
but still keep the variables?

I''ve tried:

for o in xrange(25):
self.checkbox[o] = ...

which didn''t work, and

for o in xrange(25):
self.checkbox[''''%d''%(o)] = ...

which also didn''t work.

Both give the error message: "Attribute error: Main.App has no
attribute "checkbox"", which clearly indicates that I''m not keeping
the "variability" aspect I want.

Is there a way?

I appreciate any and all answers!



将复选框存储在数组或散列/字典中。如果那个'b
不实用,那么

您可以使用字符串来构建代码并使用eval来执行字符串

作为代码。例如:


我在范围内(10):

code ="%d +%d" %(i,i)

打印评估(代码)


Either store the checkboxes in an array or hash/dictionary. If that''s
not practical, then
You can use strings to build the code and use eval to execute the string
as code. Ex:

for i in range(10):
code = "%d + %d" % (i, i)
print eval(code)


这篇关于迭代创建变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆