Two ways to create a two-dimensional array Python

Disclaimer: This article is a blogger original article, follow the CC 4.0 BY-SA copyright agreement, reproduced, please attach the original source link and this statement.
This link: https://blog.csdn.net/qq_41596915/article/details/98087688
# 列表推导式
# 5*5二维列表
b = [[0 for i in range(5)] for x in range(5)]

# for循环 避免浅拷贝
# 5*5二维列表
a = []
for i in range(5):
    a.append([])
    for j in range(5):
        a[i].append(0)

Out:
[[0, 0, 0, 0, 0], [0, 0, 0, 0, 0], [0, 0, 0, 0, 0], [0, 0, 0, 0, 0], [0, 0, 0, 0, 0]]

Guess you like

Origin blog.csdn.net/qq_41596915/article/details/98087688