簡體   English   中英

替代矢量<vector<int> > 在 python </vector<int>

[英]Substitute for vector<vector<int>> in python

python 中 vector<vector>a 的替代品是什么?

這行得通嗎?

a=[[]]

您可以通過以下方式將其指定為 int 列表的列表:

from typing import List

a = [[]]  # type: List[List[int]]

如果您嘗試 append 一個字符串,Python 本身不會抱怨:

a[0].append('hello')

但是在它上面運行mypy會抱怨:

c:\tmp> mypy vecint.py
vecint.py:6: error: Argument 1 to "append" of "list" has incompatible type "str"; expected "int"
Found 1 error in 1 file (checked 1 source file)

這不相等,因為len(a) = 1。在 C++ 中,空向量的大小為 0。

沒有簡單的解決方案,因為在 Python 中,空列表不是列表的列表。 這只是一個普通的列表。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM