list comprehension in python

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


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

SyntaxError: invalid syntax

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


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

[b]another list comprehension:[/b]
for i,x,y in points:

[b]result[/b]:SyntaxError: invalid syntax

[b]try again:[/b]
for i,(x,y) in points:


[b]conclusion[/b]: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.
發佈了48 篇原創文章 · 獲贊 1 · 訪問量 3312
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章