开发者

Convert list to sequence of variables

开发者 https://www.devze.com 2022-12-26 23:44 出处:网络
I was wondering if this was possible... I have a sequence of variables that have to be assigned to a do.something (a, b) a and b variables accordingly.

I was wondering if this was possible...

I have a sequence of variables that have to be assigned to a do.something (a, b) a and b variables accordingly.

Something like this:

# # 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开发者_StackOverflow社区.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?)  

Any ideas?


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

The * means "use the values in this tuple as the arguments". Hint: you can do the same with keyword arguments by using ** and a dict.

0

精彩评论

暂无评论...
验证码 换一张
取 消