Python equivalent for C++ STL vector/list containers
Question:
Is there something similar in Python that I would use for a container that’s like a vector and a list?
Any links would be helpful too.
Answers:
You can use the inbuilt list – underlying implementation is similar to C++ vector. Although some things differ – for example, you can put objects of different type in one and the same list.
http://effbot.org/zone/python-list.htm
N.B.: Please keep in mind that vector and list are two very different data structures. List are heterogeneous, i.e. can store different object types, while C++ vectors are homogeneous. The data in vectors is stored in linear arrangement whereas in list is a collection of references to the type and the memory address of the variables.
Lists are sequences.
see http://docs.python.org/tutorial/datastructures.html
append is like push_back, see the other methods as well.
Have a look at Python’s datastructures page. Here’s a rough translation:
- () => boost::Tuple (with one important distinction, you can’t reassign values in a Python tuple)
- [] => std::vector (as the comments have aluded towards, lacks memory characteristics associated with vectors)
- [] => std::list
- {} => tr1::unordered_map or boost::unordered_map (essentially a hash table)
- set() => std::set
py
cpp
deque
deque
PriorityQueue (or you may use heapq)
priorityqueue
set
unordered_set
list
vector
defaultdict(int)
unordered_map
list
stack
deque
queue
dict .get(val,0)
unordered_map
in py >= 3.7, dict remember insert order. https://stackoverflow.com/a/51777540/13040423
In case you need TreeMap / TreeSet
The cstl
library wrapped commonly used C++ STL libraries including vector
, unordered_map
, and unordered_set
for Python. It uses purely C++ implementation and does not have the copy-on-write issue that happens in all python objects. See
https://github.com/fuzihaofzh/cstl for how to install and use.
Install it from pip
pip install cstl
Convert python objects into cstl objects:
import cstl
# Directly covert containers from python
v = cstl.frompy({"1":[1,2,3], "2":[4,5,6]}) # convert python object to cstl object
v["1"][2] = 10 # access cstl object
pv = cstl.topy(v) # convert cstl object to python object
print(pv)
Is there something similar in Python that I would use for a container that’s like a vector and a list?
Any links would be helpful too.
You can use the inbuilt list – underlying implementation is similar to C++ vector. Although some things differ – for example, you can put objects of different type in one and the same list.
http://effbot.org/zone/python-list.htm
N.B.: Please keep in mind that vector and list are two very different data structures. List are heterogeneous, i.e. can store different object types, while C++ vectors are homogeneous. The data in vectors is stored in linear arrangement whereas in list is a collection of references to the type and the memory address of the variables.
Lists are sequences.
see http://docs.python.org/tutorial/datastructures.html
append is like push_back, see the other methods as well.
Have a look at Python’s datastructures page. Here’s a rough translation:
- () => boost::Tuple (with one important distinction, you can’t reassign values in a Python tuple)
- [] => std::vector (as the comments have aluded towards, lacks memory characteristics associated with vectors)
- [] => std::list
- {} => tr1::unordered_map or boost::unordered_map (essentially a hash table)
- set() => std::set
py | cpp |
---|---|
deque | deque |
PriorityQueue (or you may use heapq) | priorityqueue |
set | unordered_set |
list | vector |
defaultdict(int) | unordered_map |
list | stack |
deque | queue |
dict .get(val,0) | unordered_map |
in py >= 3.7, dict remember insert order. https://stackoverflow.com/a/51777540/13040423
In case you need TreeMap / TreeSet
The cstl
library wrapped commonly used C++ STL libraries including vector
, unordered_map
, and unordered_set
for Python. It uses purely C++ implementation and does not have the copy-on-write issue that happens in all python objects. See
https://github.com/fuzihaofzh/cstl for how to install and use.
Install it from pip
pip install cstl
Convert python objects into cstl objects:
import cstl
# Directly covert containers from python
v = cstl.frompy({"1":[1,2,3], "2":[4,5,6]}) # convert python object to cstl object
v["1"][2] = 10 # access cstl object
pv = cstl.topy(v) # convert cstl object to python object
print(pv)