python 如何定义动态二维数组

Python012

python 如何定义动态二维数组,第1张

Python中创建二维列表/数组,即创建一个list,并且这个list的元素还是list。可以用列表解析的方法实现。

创建例子如下:

2d_list = [[0 for col in range(cols)] for row in range(rows)]

其中cols, rows变量替换为你需要的数值即可,例如:

2d_list = [[0 for col in range(9)] for row in range(9)]# 9*9的二维列表

str='today I will go home'

list=[]

for i in str:

    if str.count(i)==1:

        list.append(i)

print(list)

运行结果: