简体   繁体   中英

Substitute for vector<vector<int>> in python

What is the substitute of vector<vector>a in python?

Will this work?

a=[[]]

You can specify it as a list of list of int by:

from typing import List

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

Python itself will not complain if you try to append a string:

a[0].append('hello')

but running mypy over it will complain:

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)

This isn't equal, because len(a) = 1. In C++, an empty vector has size 0.

There's no simple solution, because in Python an empty list is not a list of lists. It's just a plain list.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM