python创建一个二维列表
方法一:利用for-in语句来生成一个二维列表
a = []
2 for i in range(10):
3 a.append([])
4 for j in range(10):
5 a[i].append(0)
6
7 print(a)
8 a[0][1] = 1
9 print(a)
运行结果
[[0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0,
0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0,
0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0
, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0,
0], [0, 0, 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]]
代码解析
第一次循环是创建10行的空列表,第二个循环是在每行的空列表中添加10个数据,也就是添加10列使其成为10x10的二维列表
方法二:for语句
b = [[0 for i in range(10)] for i in range(10)]
print(b)
运行结果:
[[0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0,
0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0,
0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0
, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0,
0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0]]
代码解析:
for语句前的表达式是循环体,这个知识点可能大家都没太过于注意。
[0 for i in range(10)]
这部分语句是为了生成一个1x10的一维列表,再对后面的可迭代对象进行迭代,最终生成了一个10x10的二维列表
python创建一个二维列表的更多相关文章
- JAVA中如何创建一个二维数组,然后给二维数组赋值!
普通的赋值是:int[][] i = {{1,2,3},{2,3,4},{1,3,4}}; 如果是其他情况可以这样:比如: import java.util.* public class TT(){ ...
- python创建与遍历List二维列表
python创建与遍历List二维列表 觉得有用的话,欢迎一起讨论相互学习~Follow Me python 创建List二维列表 lists = [[] for i in range(3)] # 创 ...
- 在python中定义二维数组
发表于 http://liamchzh.0fees.net/?p=234&i=1 一次偶然的机会,发现python中list非常有意思. 先看一段代码 [py]array = [0, 0, 0 ...
- 用python构建一个多维维数组
用python构建一个二维数组 解法? 方法1: num_list=[0]*x//表示位创建一个一维数组为num_lis[x],且数组中的每一项都为0 num_list=[[0]*x for i in ...
- python中的二维数组90度旋转
data=[[col for col in range(4)] for raw in range(4)] #创建一个二维数组 for n in data: print (n) print('\n') ...
- Python创建二维列表的正确姿势
Python创建二维列表的正确姿势 简介 Python中没有数组,使用list结构代替,并且list结构的功能更加强大: 支持动态扩容,无需担心元素过量 对list内的元素类型不做一致性约束 提供丰富 ...
- python构造二维列表以及排序字典
1. 构造二维列表: 比如我现在需要一个100*100的二维列表: a = [] for i in range(100): a.append([]) for j in range(100): a[i] ...
- Python 二维列表
一维列表,可以使用 * 快速创建list1=[0]*Width r = [0]*5 print r r[1]= 1 print r [0, 0, 0, 0, 0] [0, 1, 0, 0, 0] 扩展 ...
- Python笔记25-----------创建二维列表【浅copy】和转置
一.创建二维列表 1.二维列表创建第二维的时候,如果采用*2这种方式,这是一种浅复制的方式,同时引用到同一个list,如上图的C. 这种形式,不方便修改C[ i ][ j ]的数据,如果改C[ 0 ] ...
随机推荐
- es6 curry function
es6 curry function // vuex getters export const getAdsFilterConfig = (state) => (spreader) => ...
- MBP 屏幕分辨率 All In One
MBP 屏幕分辨率 All In One screen size bug https://stackoverflow.com/questions/65462643/how-to-get-the-rea ...
- prefetch & preload & prerender & dns-prefetch & preconnect
prefetch & preload & prerender & dns-prefetch & preconnect performance optimization ...
- How to build a sortable table in native js?
How to build a sortable table in native/vanilla js? H5 DnD https://developer.mozilla.org/zh-CN/docs/ ...
- Parcel all in one
Parcel all in one Parcel https://parceljs.org/ # cli $ yarn global add parcel-bundler $ npm install ...
- Gatsby Themes
Gatsby Themes React & SSR gatsby-config.js refs https://www.gatsbyjs.com/docs/themes/ https://ww ...
- how to disabled prefers-color-scheme in js & dark theme
how to disabled prefers-color-scheme in js dark theme https://developer.mozilla.org/en-US/docs/Web/C ...
- Base 64 & URL & blob & FileReader & createObjectURL
Base 64 & URL & blob & FileReader & createObjectURL /** * let blob = item.getAsFile( ...
- Chrome & console.log & color & js
Chrome & console.log & color & js console.log & color // OK log(`%cchat_list =\n%c${ ...
- vue的filter用法,检索内容
var app5 = new Vue({ el: '#app5', data: { shoppingList: [ "Milk", "Donuts", &quo ...