0

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.

Henri Menke
  • 10,705
  • 1
  • 24
  • 42
  • _Why_ is a list of lists not what you want? A `std::vector` is (very approximately) a `list`, and `std::pair` is essentially a 2-tuple, so you'd normally use a Python `tuple` if it's immutable and a `list` otherwise. – Useless Nov 27 '17 at 18:17

1 Answers1

3

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))]
m_callens
  • 6,100
  • 8
  • 32
  • 54
  • Thank you. I'm new in Python and the concept of Data Structures in python is new for me (I always think from the generics or STL point of view). But is there some way to initialize this structure in empty form ? – Suleyman Suleymanzade Nov 27 '17 at 18:36