python - tuple sorting of graphs by certain list(edgeweight) -


i have developed tuple looks follows:

(['austin', 'austin', 'houston', 'dallas', 'dallas', 'san antonio'], ['san antonio', 'el paso', 'austin', 'austin', 'houston', 'el paso'], [1, 8, 3, 7, 3, 8]) 

each city name label , numbers in third list represent edge weights of vertices. how able sort tuple third set allow vertices sorted according weight. example,

austin--san antonio 1 houston--austin 3 dallas--houston 3 dallas--austin 7 austin--el paso 8 san antonio--el paso 8 

using zip , sorted functions:

in [54]: srtd = sorted(zip(*t), key=lambda x: x[-1])  in [55]: srtd out[55]:  [('austin', 'san antonio', 1),  ('houston', 'austin', 3),  ('dallas', 'houston', 3),  ('dallas', 'austin', 7),  ('austin', 'el paso', 8),  ('san antonio', 'el paso', 8)] 

where t tuple. if need convert result tuple:

in [56]: tuple(zip(*srtd)) out[56]:  (('austin', 'houston', 'dallas', 'dallas', 'austin', 'san antonio'),  ('san antonio', 'austin', 'houston', 'austin', 'el paso', 'el paso'),  (1, 3, 3, 7, 8, 8)) 

Comments

Popular posts from this blog

python 3 IndexError: list index out of range -

android - How to save instance state of selected radiobutton on menu -

IF statement in MySQL trigger -