C++ STL 向量/列表容器的 Python 等效项

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4637095/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-18 16:40:53  来源:igfitidea点击:

Python equivalent for C++ STL vector/list containers

c++pythonlistvectorcontainers

提问by pandoragami

Is there something similar in Pythonthat I would use for a container that's like a vector and a list?

Python中是否有类似的东西可以用于像向量和列表这样的容器?

Any links would be helpful too.

任何链接也会有帮助。

采纳答案by Johan Kotlinski

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.

您可以使用内置列表 - 底层实现类似于 C++ 向量。尽管有些事情有所不同 - 例如,您可以将不同类型的对象放在同一个列表中。

http://effbot.org/zone/python-list.htm

http://effbot.org/zone/python-list.htm

回答by Umut Tabak

Lists are sequences.

列表是序列。

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

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

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

append 就像 push_back,其他方法也一样。

回答by wheaties

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

看看 Python 的数据结构页面。这是一个粗略的翻译:

  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
  1. () => boost::Tuple(有一个重要的区别,你不能在 Python 元组中重新赋值)
  2. [] => std::vector (正如评论所暗示的那样,缺乏与向量相关的内存特征)
  3. [] => std::list
  4. {} => tr1::unordered_map 或 boost::unordered_map(本质上是一个哈希表)
  5. set() => std::set

回答by Paul

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

Python 还具有作为标准库的一部分的数组类型,它更有效并且成员类型受到限制。

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.

如果您需要认真对待大型向量/数组的有效操作,您也可以查看numpy(不是标准库的一部分)。