3

I need to store start and end indexes of certain substrings. I need to do this in python. What is the python equivalent for c++ vector of pairs?

Buddha
  • 51
  • 1
  • 5
  • Possible duplicate of [Python equivalent for C++ STL vector/list containers](https://stackoverflow.com/questions/4637095/python-equivalent-for-c-stl-vector-list-containers) – idjaw Jun 21 '17 at 03:12
  • visit here [link](https://docs.python.org/3/library/stdtypes.html) ... look for lists and tuples ... **please cultivate a habit of finding answers by yourself first ... search for documentations if you are learning a new programming language** – รยקคгรђשค Jun 21 '17 at 03:18
  • No, i am unable to find python equivalent of cpp stl vector of pairs. Please read the question then answer. – Buddha Jun 21 '17 at 03:26
  • A list of tuples. A pair is a tuple with 2 elements (c++ and haskell both agree this). Since you dont want the elements to be mutable, tuple is perfect choice. – Paul Rooney Jun 21 '17 at 03:40

2 Answers2

2

I would suggest storing it in a dictionary (Hashmap)

input = ['str1', 'str2', 'str3']
stored_as = {'str1': {'start': 1, 'end': 2}, 'str2': {'start': 0, 'end': 2}, 'str3': {'start': 1, 'end': 1}}

This gives you a better representation. If you are tight on space, then you can store it as either of the following:

stored_as = [(1,2), (0,2), (1,1)]

or

stored_as = [[1,2], [0,2], [1,1]]
2

If you use slice objects, you can use them to select the substrings directly:

In [924]: al = [slice(0,3), slice(2,5), slice(5,10)]
In [925]: astr = 'this is a long enough string'
In [926]: [astr[s] for s in al]
Out[926]: ['thi', 'is ', 'is a ']

Or with a list of tuples:

In [927]: at = [(0,3), (2,5), (5,10)]
In [928]: [astr[s[0]:s[1]] for s in at]
Out[928]: ['thi', 'is ', 'is a ']

They could even be named tuples. or a list of lists.

We can even hide that slice iteration with an itemgetter:

In [933]: import operator
In [934]: f=operator.itemgetter(*al)
In [935]: f
Out[935]: operator.itemgetter(slice(0, 3, None), slice(2, 5, None), slice(5, 10, None))
In [936]: f(astr)
Out[936]: ('thi', 'is ', 'is a ')

This list of slices could also contain scalar indexes:

In [945]: al = [0, slice(5,7), slice(10,14), -1]
In [946]: operator.itemgetter(*al)(astr)
Out[946]: ('t', 'is', 'long', 'g')
hpaulj
  • 221,503
  • 14
  • 230
  • 353