Python equivalent for C++ STL vector/list containers

前端 未结 4 642
小鲜肉
小鲜肉 2020-12-29 06:29

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.

相关标签:
4条回答
  • 2020-12-29 06:36

    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.

    0 讨论(0)
  • 2020-12-29 06:38

    Lists are sequences.

    see http://docs.python.org/tutorial/datastructures.html

    append is like push_back, see the other methods as well.

    0 讨论(0)
  • 2020-12-29 06:39

    Have a look at Python's datastructures page. Here's a rough translation:

    1. () => boost::Tuple (with one important distinction, you can't reassign values in a Python tuple)
    2. [] => std::vector (as the comments have aluded towards, lacks memory characteristics associated with vectors)
    3. [] => std::list
    4. {} => tr1::unordered_map or boost::unordered_map (essentially a hash table)
    5. set() => std::set
    0 讨论(0)
  • 2020-12-29 06:45

    Python also has as part of the standard library an array type which is more efficient and the member type is constrained.

    You may also look at numpy (not part of the standard library) if you need to get serious about efficient manipulation of large vectors/arrays.

    0 讨论(0)
提交回复
热议问题