문제

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
도움이 되었습니까?

해결책

Simple:

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

It's called sequence unpacking.

다른 팁

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

for u,v in link_list:
     print u,v

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

u,v = (x,y)
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top