只是一个真正的快速。我有一个变量列表,仅变化了1位数。我希望我的CheckGabba1 = IntVar()
CheckGabba2 = IntVar()
CheckGabba3 = IntVar()
## etc. (x10)
## There are items in these lists (This is just to show that they are lists)
AllEventsGabba = []
SelectedEventsGabba = []
NumEvents = 10
ListIndex = 0
for EventCheck in range(NumEvents):
if (CheckGabba(ListIndex + 1)).get() == 1:
SelectedEventsGabba.append(AllEventsGabba[ListIndex])
ListIndex += 1
else:
ListIndex += 1
语句引用每个单独的变量,但是我不确定该怎么做。你能帮我吗?
(CheckGabba(ListIndex + 1)
显然{{1}}是错误的,但是我不确定需要用什么替换它来使循环自动检查每个变量,而不是硬编写(我可以这样做,但是我更愿意这样做) )。
答案 0 :(得分:0)
我不确定我是否完全理解您的问题,但是对我来说,它看起来像这样:
# Create a dict of the variables that you want to check later
CheckGabba = {i: IntVar() for i in range(10)}
# Then use a list comprehension to filter the data
SelectedEventsGabba = [event for event in CheckGabba.items() if event == 1]
答案 1 :(得分:-1)
您可以将所有CheckGabbas分组到一个列表中,并在它们上循环,例如
check_list=[CheckGabba1,CheckGabba2,CheckGabba3]
for check in check_list:
if check.get()==1:
一种替代方法是在字符串上使用exec,例如
exec "CheckGabba("+str(Listindex+1)+")"