chunjihong 发表于 2017-4-23 10:40:47

list comprehension in python

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

list comprehension:

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.
页: [1]
查看完整版本: list comprehension in python