# 嵌套列表索引

` `some_list = 4 * [(4 * [0])] for i in range(3): for j in range(3): some_list[i+1][j+1] = 1 for i in range(4): print(some_list[i])` `

` `[0, 0, 0, 0] [0, 1, 1, 1] [0, 1, 1, 1] [0, 1, 1, 1]` `

` `[0, 1, 1, 1] [0, 1, 1, 1] [0, 1, 1, 1] [0, 1, 1, 1]` `

### 2 Solutions collect form web for “嵌套列表索引”

` `>>> a = 1 >>> b = a >>> a = 2 >>> print b 1` `

` `>>> a = [1] >>> b = a >>> a[0] = 2 >>> print b [2]` `

` `>>> a = 4 * [0] >>> a [0, 0, 0, 0] >>> [id(v) for v in a] [33302480, 33302480, 33302480, 33302480] >>> a[0] = 1 >>> a [1, 0, 0, 0]` `

` `>>> a[0][0] = 1 >>> a [[1, 0, 0, 0], [1, 0, 0, 0], [1, 0, 0, 0], [1, 0, 0, 0]]` `

` `>>> some_list = [(4 * [0]) for _ in range(4)]` `

` `In [151]: some_list = 4 * [(4 * [0])] In [152]: [id(x) for x in some_list] Out[152]: [148641452, 148641452, 148641452, 148641452] In [160]: some_list[0][1]=5 #you think you changed the list at index 0 here In [161]: some_list Out[161]: [[0, 5, 0, 0], [0, 5, 0, 0], [0, 5, 0, 0], [0, 5, 0, 0]] #but all lists are changed` `

` `In [156]: some_list=[[0]*4 for _ in range(4)] In [157]: some_list Out[157]: [[0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0]] In [158]: [id(x) for x in some_list] Out[158]: [148255436, 148695180, 148258380, 148255852] In [163]: some_list[0][1]=5 In [164]: some_list Out[164]: [[0, 5, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0]] #works fine in this case` `
• 了解python的主要方法
• 从Python 3的网站下载文件
• 用于Python 3.x的MySQL-db库？
• python 3中的yield生成器中没有next（）函数
• except：除了Exception之外的区别：在Python中
• 什么是在lambda自动元组解压缩好的python3等价物？
• Python3项目删除__pycache__文件夹和.pyc文件
• 如何testingPython 3.4 asyncio代码？
• 此应用程序无法启动，因为它无法find或加载Qt平台插件“cocoa”
• “Unicode错误”unicodeescape“编解码器无法解码字节...无法在Python 3中打开文本文件
• Python安全的方法来获取嵌套字典的值