---
title: "Python初始化多维数组"
author: "fiftyk"
date: "2012-10-08T17:20:18Z"
tags: []
categories: ["编程语言"]
slug: "python"
draft: false
lastmod: "2012-10-08T17:20:18Z"
id: 1693782
url: "https://fiftyk.iteye.com/blog/1693782"
---

```
#方法一
twoD = [[for col in range(5) for row in range(3)]]
#>>[[0, 0, 0, 0, 0], [0, 0, 0, 0, 0], [0, 0, 0, 0, 0]]
twoD[0][0] = 1
#>>[[0, 1, 0, 0, 0], [0, 0, 0, 0, 0], [0, 0, 0, 0, 0]]
twoD[1] = [1]
#>>[[0, 1, 0, 0, 0], [1], [0, 0, 0, 0, 0]]


#方法2
twoD = [[0] * 5] * 3
#>>[[0, 0, 0, 0, 0], [0, 0, 0, 0, 0], [0, 0, 0, 0, 0]]
twoD[0][0] = 1
#>>[[1, 0, 0, 0, 0], [1, 0, 0, 0, 0], [1, 0, 0, 0, 0]]
twoD[1] = [1]
#>>[[1, 0, 0, 0, 0], [1], [1, 0, 0, 0, 0]]
```

 参考地址：[http://www.jb51.net/article/15716.htm](http://www.jb51.net/article/15716.htm)