专业做网站优化排名,微信网站可以免费做么,广安市建设局新网站,湘潭学校网站建设 磐石网络专注一、列表
1、理解
列表是一个值#xff0c;包含由多个值构成的序列
2、元素查找
1#xff09;索引--取列表中的单个值
正数索引#xff1a;同c语言中的数组
spam [[1,2,3,4],[cat,dog]]
print(spam[0][1])
#结果#xff1a;2
负数索引包含由多个值构成的序列
2、元素查找
1索引--取列表中的单个值
正数索引同c语言中的数组
spam [[1,2,3,4],[cat,dog]]
print(spam[0][1])
#结果2
负数索引表示从右往左取值
spam [cat,dog,zebra,monkey]
spam[-1] ---- monkey
spam[-2] ---- zebra
2切片--去列表中的多个值
结果为一个列表
1spam[整数1:整数2] : 整数1表示切片开始处的索引整数2表示切片结束出的索引不包括该位置元素
spam [cat,dog,zebra,monkey]
spam[0:4] --- [cat,dog,zebra,monkey]
spam[1:3] --- [dog,zebra]
2整数1不写时表示从0开始整数2不写时表示直到列表的末尾
spam [cat,dog,zebra,monkey]
spam[:4] --- [cat,dog,zebra,monkey]
spam[1:] --- [dog,zebra,monkey]
3、列表长度
列表中元素个数表示列表的长度
取上述spam列表其len(spam) 4
4、改变元素的值
同数组
5、连接与复制
列表用于连接*整数表示复制
6、删除元素
使用del语句
spam [cat,dog,zebra,monkey]
del spam[0] ---- spam [dog,zebra,monkey]
7、遍历列表
1使用rangerange本质上返回的是列表值使用for i in range(len(spam)):来遍历列表
2使用enumerate使用for index,item in enumerate(spam): 来遍历列表。其中idex表示列表索引item表示该索引对应的元素当我同时需要列表索引和其对应元素时用该方法方便
spam [cat,dog,zebra,monkey]
for index,item in enumerate(spam):print(index,item) 8、判断元素是否在列表中
使用in和not in操作符
spam [cat,dog,zebra,monkey]
cat in spam ---- True
howdy not in spam ---- True9、多重赋值
保证变量个数与列表长度必须一致
cat [fat,black,loud]
size,color,disposition cat10、random.choice()和random.shuffle()与列表一起使用
1random.choice()表示随机选择列表中某一个表项并返回
2random.shuffle()表示就地对列表进行重新排序不产生新列表
11、列表中的方法(就地解决)
1查找--index()
传入一个值若该值在列表中则返回其索引不再则报ValueError错误
spam [cat,dog,zebra,monkey]
try:print(spam.index(cat))print(spam.index(cow))
except ValueError:print(Index Error) 2增加--insert()append()
insert(参数1,参数2)参数1表示新值的索引参数2表示参数要插入的新值
append(元素)只能向列表尾部进行添加元素
spam [cat,dog,zebra,monkey]
spam.insert(1,world)
print(spam)
spam.append(cow)
print(spam)3删除--remove()
remove(元素)若元素多次出现则只删除第一次出现的值若该元素不存在则报错ValueError
kips如果我已经知道该元素索引了那么我可以用del来直接删除该元素
4排序--sort()
默认顺序排序若想逆序排序则sort(reverseTrue)
列表中元素类型一致才可以排序否则报错TypeError
对字符串排序时使用的是ASCⅡ字符顺序方式因此有大小写区别
若按照普通字典顺序排序则写为sort(keystr.lower)
5反转--reverse()
12、序列数据类型
包括列表字符串range()返回的范围对象以及元组
只要是序列数据类型前边提到的对列表进行查看的操作都可以使用
因为字符串是常量不可变因此不能对字符串进行修改操作若想修改字符串则使用切片和连接构造一个“新的”字符串
二、元组
1、理解 元组是列表数据类型的不可变形式 将列表中的[]改为()即为元组 当元组中只有一个元素时在末尾加一个逗号告诉编译器这是一个元组而不是其他类型 print(type((1)))
print(type((1,))) 2、元组、列表类型转化
使用tuple()和list()函数实现
3、可变值与不可变值
对于可变值而言ab说明id(a)id(b)
对于不可变值而言ab说明id(a)!id(b)
函数传参同上
4、copy模块
使用copy.copy()函数可以用来复制列表或字典的可变值相当于创建了一个新的列表或字典
使用copy.deepcopy()函数用来处理列表中嵌套列表的情况
三、小程序Conway的生命游戏
import random,time,copy
width 5
height 5nextCells []
for i in range(height):row []for y in range(width):if random.randint(0,1) 0:row.append(#)else:row.append( )nextCells.append(row)while True:print(\n\n\n\n\n)currentCell copy.deepcopy(nextCells)for i in range(height):for j in range(width):print(currentCell[i][j],end)print()for x in range(width):for y in range(height):leftCoord (x-1) % widthrightCoord (x1) % widthupCoord (y-1) % heightdownCoord (y1) % heightnumNeighbors 0if currentCell[leftCoord][upCoord] #:numNeighbors 1if currentCell[x][upCoord] #:numNeighbors 1if currentCell[rightCoord][upCoord] #:numNeighbors 1if currentCell[leftCoord][y] #:numNeighbors 1if currentCell[rightCoord][y] #:numNeighbors 1if currentCell[leftCoord][downCoord] #:numNeighbors 1if currentCell[x][downCoord] #:numNeighbors 1if currentCell[rightCoord][downCoord] #:numNeighbors 1if currentCell[x][y] # and (numNeighbors 2 or numNeighbors 3):nextCells[x][y] #elif currentCell[x][y] and numNeighbors 3:nextCells[x][y] #else:nextCells[x][y] time.sleep(1)
文章转载自: http://www.morning.tbjb.cn.gov.cn.tbjb.cn http://www.morning.bmts.cn.gov.cn.bmts.cn http://www.morning.mwns.cn.gov.cn.mwns.cn http://www.morning.bzlfw.cn.gov.cn.bzlfw.cn http://www.morning.tlbdy.cn.gov.cn.tlbdy.cn http://www.morning.pqqzd.cn.gov.cn.pqqzd.cn http://www.morning.cwrpd.cn.gov.cn.cwrpd.cn http://www.morning.gpxbc.cn.gov.cn.gpxbc.cn http://www.morning.nsncq.cn.gov.cn.nsncq.cn http://www.morning.tgczj.cn.gov.cn.tgczj.cn http://www.morning.dwkfx.cn.gov.cn.dwkfx.cn http://www.morning.tmrjb.cn.gov.cn.tmrjb.cn http://www.morning.xqgtd.cn.gov.cn.xqgtd.cn http://www.morning.dbrdg.cn.gov.cn.dbrdg.cn http://www.morning.dqrpz.cn.gov.cn.dqrpz.cn http://www.morning.rrqgf.cn.gov.cn.rrqgf.cn http://www.morning.tnjz.cn.gov.cn.tnjz.cn http://www.morning.rxxdk.cn.gov.cn.rxxdk.cn http://www.morning.wrlxy.cn.gov.cn.wrlxy.cn http://www.morning.mphfn.cn.gov.cn.mphfn.cn http://www.morning.jzfxk.cn.gov.cn.jzfxk.cn http://www.morning.bzsqr.cn.gov.cn.bzsqr.cn http://www.morning.trmpj.cn.gov.cn.trmpj.cn http://www.morning.hqbk.cn.gov.cn.hqbk.cn http://www.morning.rynqh.cn.gov.cn.rynqh.cn http://www.morning.jwdys.cn.gov.cn.jwdys.cn http://www.morning.nydgg.cn.gov.cn.nydgg.cn http://www.morning.skdhm.cn.gov.cn.skdhm.cn http://www.morning.jppdk.cn.gov.cn.jppdk.cn http://www.morning.fqhbt.cn.gov.cn.fqhbt.cn http://www.morning.jgcrr.cn.gov.cn.jgcrr.cn http://www.morning.lkhfm.cn.gov.cn.lkhfm.cn http://www.morning.glpxx.cn.gov.cn.glpxx.cn http://www.morning.gtwtk.cn.gov.cn.gtwtk.cn http://www.morning.caswellintl.com.gov.cn.caswellintl.com http://www.morning.fmswb.cn.gov.cn.fmswb.cn http://www.morning.wpcfm.cn.gov.cn.wpcfm.cn http://www.morning.rqmqr.cn.gov.cn.rqmqr.cn http://www.morning.rlhgx.cn.gov.cn.rlhgx.cn http://www.morning.qpfmh.cn.gov.cn.qpfmh.cn http://www.morning.lfxcj.cn.gov.cn.lfxcj.cn http://www.morning.qbmpb.cn.gov.cn.qbmpb.cn http://www.morning.dktyc.cn.gov.cn.dktyc.cn http://www.morning.ishoufeipin.cn.gov.cn.ishoufeipin.cn http://www.morning.zttjs.cn.gov.cn.zttjs.cn http://www.morning.pznnt.cn.gov.cn.pznnt.cn http://www.morning.ghxzd.cn.gov.cn.ghxzd.cn http://www.morning.bpmdz.cn.gov.cn.bpmdz.cn http://www.morning.jpydf.cn.gov.cn.jpydf.cn http://www.morning.nlygm.cn.gov.cn.nlygm.cn http://www.morning.hmqmm.cn.gov.cn.hmqmm.cn http://www.morning.wnnlr.cn.gov.cn.wnnlr.cn http://www.morning.kkdbz.cn.gov.cn.kkdbz.cn http://www.morning.fcrw.cn.gov.cn.fcrw.cn http://www.morning.ffksr.cn.gov.cn.ffksr.cn http://www.morning.wkknm.cn.gov.cn.wkknm.cn http://www.morning.tpbhf.cn.gov.cn.tpbhf.cn http://www.morning.qfkxj.cn.gov.cn.qfkxj.cn http://www.morning.sphft.cn.gov.cn.sphft.cn http://www.morning.dlhxj.cn.gov.cn.dlhxj.cn http://www.morning.yxgqr.cn.gov.cn.yxgqr.cn http://www.morning.ynstj.cn.gov.cn.ynstj.cn http://www.morning.skpdg.cn.gov.cn.skpdg.cn http://www.morning.yxwnn.cn.gov.cn.yxwnn.cn http://www.morning.rglzy.cn.gov.cn.rglzy.cn http://www.morning.lzqxb.cn.gov.cn.lzqxb.cn http://www.morning.rmqmc.cn.gov.cn.rmqmc.cn http://www.morning.i-bins.com.gov.cn.i-bins.com http://www.morning.hmlpn.cn.gov.cn.hmlpn.cn http://www.morning.cyyhy.cn.gov.cn.cyyhy.cn http://www.morning.zzhqs.cn.gov.cn.zzhqs.cn http://www.morning.wcqkp.cn.gov.cn.wcqkp.cn http://www.morning.qxwgx.cn.gov.cn.qxwgx.cn http://www.morning.nrqnj.cn.gov.cn.nrqnj.cn http://www.morning.yzzfl.cn.gov.cn.yzzfl.cn http://www.morning.gcfg.cn.gov.cn.gcfg.cn http://www.morning.wcjk.cn.gov.cn.wcjk.cn http://www.morning.pdmc.cn.gov.cn.pdmc.cn http://www.morning.tgpgx.cn.gov.cn.tgpgx.cn http://www.morning.sjwqr.cn.gov.cn.sjwqr.cn