Search code examples
pythondata-structurespython-collections

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.


Solution

  • 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))]