How to split tuple with parentheses in Python?

大城市里の小女人 提交于 2020-02-06 06:33:07

问题


I have built-in tuple which looks like (u,v). They are generated by Networkx and they show links in a graph. I make a list out of the called link_list.

I have to split the tuple such that the outcome would be: u , v

I tried divmod but it doesn't give the right answer.

for link in link_list:
    u,v = divmod(*link)
    print u,v

回答1:


Simple:

for link in link_list:
    u, v = link
    print u, v

It's called sequence unpacking.




回答2:


you can get the tuple into individual variables in the for statement as following:

for u,v in link_list:
     print u,v



回答3:


If you have a tuple (x,y), and you wish to destructure it to two variables, the syntax is simply:

u,v = (x,y)


来源:https://stackoverflow.com/questions/8806101/how-to-split-tuple-with-parentheses-in-python

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!