python 创建二维数组的方法

废话不多说,直接上代码:

#coding=utf-8

def two_di_demo1():    a=[]    for i in range(10):        a.append([])        for j in range(10):            a[i].append(0)    print(a)

def two_di_demo2():    a=[]    for i in range(10):        a.append([])        for j in range(10):            a[i].append(0)    print(a)    b = [[0] * 10] * 10    print(b)    print(a==b)    b[1][0]=1    print(b)

def two_di_demo3():    c=[[0 for i in range(10)] for i in range(10)]    print(c)

if __name__=="__main__":    two_di_demo1()    two_di_demo2()    two_di_demo3()

运行结果如下:

F:\dev\python\python.exe F:/pyCharm/pratice/twodi_list/demo_1.py
[[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, 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], [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, 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]]
[[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, 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], [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, 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]]
[[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, 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], [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, 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]]
True
[[1, 0, 0, 0, 0, 0, 0, 0, 0, 0], [1, 0, 0, 0, 0, 0, 0, 0, 0, 0], [1, 0, 0, 0, 0, 0, 0, 0, 0, 0], [1, 0, 0, 0, 0, 0, 0, 0, 0, 0], [1, 0, 0, 0, 0, 0, 0, 0, 0, 0], [1, 0, 0, 0, 0, 0, 0, 0, 0, 0], [1, 0, 0, 0, 0, 0, 0, 0, 0, 0], [1, 0, 0, 0, 0, 0, 0, 0, 0, 0], [1, 0, 0, 0, 0, 0, 0, 0, 0, 0], [1, 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, 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, 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, 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, 0, 0, 0, 0, 0, 0, 0, 0, 0]]

Process finished with exit code 0

原文地址:https://www.cnblogs.com/linwenbin/p/10491614.html

时间: 2024-10-14 12:52:58

python 创建二维数组的方法的相关文章

python创建二维数组

1 c=[[0]*3 for i in range(3)] 2 3 c=[[0 for i in range(3)] for i in range(3)]

Python的二维数组

Python一维数组初始化: >>> list=[] >>> type(list) <type 'list'> >>> list [] Python二维数组初始化: >>> lists = [[]] * 3 >>> lists [[], [], []] >>> lists[0].append(3) >>> lists [[3], [3], [3]] "[[]]

C语言 动态创建二维数组

/*C语言 如何动态创建二维数组 转化为一维数组申请数组,创建和释放都比较简单 */ #include <stdlib.h> #include <stdio.h> #include <malloc.h> #define RANK 10 #define COLUMN 7 int main() { int i,j; int (*p)[COLUMN]; //动态生成二维数组,指定列数为COLUMN,如果想改,自己该里面 //的参数,如果想定义n行2列就为: p=(int (*

动态创建二维数组

vector创建二维数组 初始化二维数组 vector<vector <int> > ivec(n ,vector<int>(m)); //n*m的二维vector vector<vector <int> > ivec(n , vector<int>(m, 0)); //n*m的二维vector,所有元素为0 动态创建m*n的二维 方法一: vector<vector <int> > ivec; ivec.re

&lt;细节&gt;动态创建二维数组

今天在做一个关于数组相似度的题目,需要动态创建二维数组,刚开始想用常规的int a[ ][ ]建立,但很快就发现行不通,这种常规数组必须在编译前指定数组的维数,而这个题目在的维数必须在运行中计算得出,于是想到采用new来建立动态二维数组,这个方法行得通,但是很麻烦,也不太利于理解,后面决定采用vector< vector< int > >方法来创建,简单了不少,下面对两种方法都进行一下总结. 1.使用vector创建二维数组 #include <iostream> #i

【C语言】动态创建二维数组

//动态创建二维数组 #include <stdio.h> #include <stdlib.h> #include <malloc.h> int main() { int i,j; int n,m; //以n行5列为例 int (*p)[5]; printf("请输入行数:"); scanf("%d",&n); p=(int(*)[5])malloc(5*n*sizeof(int)); for(i=0;i<n;i+

c++ 动态创建二维数组

为了做一个东西需要用到动态创建二维数组,特此记录. #include <iostream> #include "time.h" using namespace std; int main() { srand((unsigned)time(NULL)); int x_num; int y_num; cout<<"please input x_num:"; cin>>x_num; cout<<"please in

模板:使用new delete 创建二维数组

1 int **arr_matrix = new int*[n]; 2 for(i = 0; i < n; ++i) arr_matrix[i] = new int[n]; 3 //内容 4 for(i = 0; i < n; ++i) delete [] arr_matrix[i]; 5 delete [] arr_matrix; 模板:使用new delete 创建二维数组

python的二维数组操作

需要在程序中使用二维数组,网上找到一种这样的用法: 1 2 3 4 5 6 #创建一个宽度为3,高度为4的数组 #[[0,0,0], # [0,0,0], # [0,0,0], # [0,0,0]] myList = [[0] * 3] * 4 但是当操作myList[0][1] = 1时,发现整个第二列都被赋值,变成 [[0,1,0], [0,1,0], [0,1,0], [0,1,0]] 为什么...一时搞不懂,后面翻阅The Python Standard Library 找到答案 lis