python 2.7 - Getting a specific value from a tuple within a list -


i have list this

tails = {(1, 352, 368), (2, 336, 368), (3, 320, 368)} 

where first value tail number, second x position , third y position. later on in code, have

for item in tail:     pygame.draw.rect(windowsurface, red, (xposition, yposition, 16, 16)) 

how second , third value specific tuple?

since you're looking learn python basics, i'll show several different ways (despite of python's "there should one-- , preferably 1 --obvious way it"):

for item in tails:     xposition = item[1]  # item index     yposition = item[2]     pygame.draw.rect(windowsurface, red, (xposition, yposition, 16, 16)) 

or

for item in tails:     ( xposition, yposition ) = item[1:]  # tuple assignment     pygame.draw.rect(windowsurface, red, (xposition, yposition, 16, 16)) 

or

for tail_num, xposition, yposition in tails:  # tuple assignment in for-loop     pygame.draw.rect(windowsurface, red, (xposition, yposition, 16, 16)) 

or

# prepare args using list comprehension rect_args_list = [ tuple(item[1:]) + ( 16, 16) item in tails ] rect_args in rect_args_list:     pygame.draw.rect(windowsurface, red, rect_args) 

Comments

Popular posts from this blog

Hatching array of circles in AutoCAD using c# -

ios - UITEXTFIELD InputView Uipicker not working in swift -

Python Pig Latin Translator -