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
Python equivalent for C++ STL vector/list containers
提问by pandoragami
采纳答案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++ 向量。尽管有些事情有所不同 - 例如,您可以将不同类型的对象放在同一个列表中。
回答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 的数据结构页面。这是一个粗略的翻译:
- () => 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
- () => boost::Tuple(有一个重要的区别,你不能在 Python 元组中重新赋值)
- [] => std::vector (正如评论所暗示的那样,缺乏与向量相关的内存特征)
- [] => std::list
- {} => tr1::unordered_map 或 boost::unordered_map(本质上是一个哈希表)
- 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(不是标准库的一部分)。

