list comprehension in python

list:
points=[('118.696', '55.016'), ('64.583', '195.986'), ('229.826', '259.417'), ('283.94', '118.447')]


list comprehension:
[Decimal(x),Decimal(y) for x,y in points]

SyntaxError: invalid syntax

[(Decimal(x),Decimal(y)) for x,y in points]


Conclusion:
if you want to return a tuple, you have to speak it out:-)

another list comprehension:
for i,x,y in points:

result:SyntaxError: invalid syntax

try again:
for i,(x,y) in points:


conclusion:if you want to get a tuple, you'd better not put it next to others, here is index i, instead make it clear that it's a tuple.

你可能感兴趣的:(python)