Python equivalent for C++ STL vector/list containers

孤人 提交于 2019-11-29 12:03:42

问题


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.


回答1:


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




回答2:


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



回答3:


Lists are sequences.

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

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




回答4:


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.



来源:https://stackoverflow.com/questions/4637095/python-equivalent-for-c-stl-vector-list-containers

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!