【wrong way:】

m=[[element] * numcols] * numrows

for example:

>>> m=[['a'] *3] * 2
>>> m
[['a', 'a', 'a'], ['a', 'a', 'a']]
>>> m[1][1]
'a'
>>> m[1][1]='b'
>>> m
[['a', 'b', 'a'], ['a', 'b', 'a']]

注意:此时m[0][1] 也被修改了, because * is copying the address of the object (list),m[0][1]和m[1][1] reference to same object.

【right way:】

m = [[element for x in range(numcols)] for y in range(numrows)]
for example:

>>> m=[['a' for x in range(3)] for y in range(2)]
>>> m
[['a', 'a', 'a'], ['a', 'a', 'a']]
>>> m[1][1]
'a'
>>> m[1][1]='b'
>>> m
[['a', 'a', 'a'], ['a', 'b', 'a']]

 

【转】How to initialize a two-dimensional array in Python?的更多相关文章

  1. 简单的算法题, Find Minimum in Rotated Sorted Array 的Python实现。

    简单的算法题, Find Minimum in Rotated Sorted Array 的Python实现. 题目: Suppose a sorted array is rotated at som ...

  2. [leetcode]Search in Rotated Sorted Array II @ Python

    原题地址:https://oj.leetcode.com/problems/search-in-rotated-sorted-array-ii/ 题意: Follow up for "Sea ...

  3. [leetcode]Remove Duplicates from Sorted Array II @ Python

    原题地址:https://oj.leetcode.com/problems/remove-duplicates-from-sorted-array-ii/ 题意: Follow up for &quo ...

  4. 「Python」Convert map object to numpy array in python 3

    转自Stackoverflow.备忘用. Question In Python 2 I could do the following: import numpy as np f = lambda x: ...

  5. leetcode 【 Find Minimum in Rotated Sorted Array 】python 实现

    题目: Suppose a sorted array is rotated at some pivot unknown to you beforehand. (i.e., 0 1 2 4 5 6 7  ...

  6. leetcode 【 Search in Rotated Sorted Array 】python 实现

    题目: Suppose a sorted array is rotated at some pivot unknown to you beforehand. (i.e., 0 1 2 4 5 6 7  ...

  7. leetcode 【 Merge Sorted Array 】python 实现

    题目: Given two sorted integer arrays A and B, merge B into A as one sorted array. Note:You may assume ...

  8. leetcode 【 Remove Duplicates from Sorted Array 】python 实现

    题目: Given a sorted array, remove the duplicates in place such that each element appear only once and ...

  9. 用list去初始化numpy的array数组 numpy的array和python中自带的list之间相互转化

    http://blog.csdn.net/baiyu9821179/article/details/53365476 a=([3.234,34,3.777,6.33]) a为python的list类型 ...

随机推荐

  1. Django学习笔记之视图高级-CSV文件生成

    生成CSV文件 有时候我们做的网站,需要将一些数据,生成有一个CSV文件给浏览器,并且是作为附件的形式下载下来.以下将讲解如何生成CSV文件. 生成小的CSV文件 这里将用一个生成小的CSV文件为例. ...

  2. 鏈接Redis報錯`AUTH` failed: ERR Client sent AUTH, but no password is set [tcp://127.0.0.1:6379]

    問題 鏈接Redis報錯`AUTH` failed: ERR Client sent AUTH, but no password is set [tcp://127.0.0.1:6379] 解決 啟動 ...

  3. request&response

    /* request&response 1.什么是HttpServletRequest HttpServletRequest对象代表客户端的请求,当客户端通过HTTP协议访问服务器时,HTTP ...

  4. CentOS install duplicity

    yum -y updateyum -y install epel-releaseyum -y install ncftp screen # Compilers and related tools:yu ...

  5. C++标准模板类库(STL)之queue初步

    1,STL里有些什么? 包括三个内容:容器.迭代器.算法. 2,容器有哪些? 有stack, vector, queue, deque, list, set, multiset, map, multi ...

  6. Elasticsearch-6.7.0系列(五)5044端口 logstash安装

    centos7环境 下载logstash wget https://artifacts.elastic.co/downloads/logstash/logstash-6.7.0.tar.gz      ...

  7. python文件管理小计

    1.读取只当路径下所有文件和文件夹 import os def file_name(file_dir): for root, dirs, files in os.walk(file_dir): pri ...

  8. nginx 用来做什么?

    代理服务端,反向代理,负载均衡. 其特点是占有内存少,并发能力强.

  9. MongoDB复制集

    1.1 MongoDB复制集简介 一组Mongodb复制集,就是一组mongod进程,这些进程维护同一个数据集合.复制集提供了数据冗余和高等级的可靠性,这是生产部署的基础. 1.1.1 复制集的目的 ...

  10. python的列表综合list-comprehension示例,及两列表取补集

    两个目的: 1. 了解了python的list comprehesion的用法 2. 了解了两个列表取交集和补集的方法 R语言取交集和补集更简单,直接有函数. perl 稍麻烦一些, 关键是用hash ...