我想知道这是否可能......
我有一系列变量必须相应地分配给do.something(a,b)a和b变量。
这样的事情:
# # Have a list of sequenced variables.
list = 2:90 , 1:140 , 3:-40 , 4:60
# # "Template" on where to assign the variables from the list.
do.something (a,b)
# # Assign the variables from the list in a sequence with possibility of "in between" functions like print and time.sleep() added.
do.something (2,90)
time.sleep(1)
print "Did something (%d,%d)" % (# # vars from list?)
do.something (1,140)
time.sleep(1)
print "Did something (%d,%d)" % (# # vars from list?)
do.something (3,-40)
time.sleep(1)
print "Did something (%d,%d)" % (# # vars from list?)
do.something (4,60)
time.sleep(1)
print "Did something (%d,%d)" % (# # vars from list?)
有什么想法吗?
答案 0 :(得分:4)
arglist = [(2, 90), (1, 140), (3, -40), (4, 60)]
for args in arglist:
do.something(*args)
time.sleep(1)
print "Did something (%d,%d)" % args
*表示“使用此元组中的值作为参数”。提示:您可以使用**和dict对关键字参数执行相同的操作。