简体   繁体   中英

vector<pair<int, pair<int,int>>> data structure in python

How to create an analogy of vector<pair<int, pair<int,int>>> of C++ data structure in python? and sort it by the first (int) parameter. I tried to use the list of lists in python, but this is not what I want. Thank you.

I was able to simulate it with a list of tuples , where each contains an int and another tuple of ints .

Example : [(1, (7,3)), (7, (2, 4)), (3, (9, 0)), (2, (43, 14))]


To sort it, set the sorting key to be the zero index of each element in the list:

>>> x = [(1, (7,3)), (7, (2, 4)), (3, (9, 0)), (2, (43, 14))]
>>> x.sort(key=lambda e: e[0])
>>> x
[(1, (7, 3)), (2, (43, 14)), (3, (9, 0)), (7, (2, 4))]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM