python数据类型之列表
列表的定义和创建
定义:[]内以逗号分隔,按照索引,存放各种数据类型,每个位置代表一个元素
网站建设、成都网站建设,成都做网站公司-创新互联建站已向近千家企业提供了,网站设计,网站制作,网络营销等服务!设计与技术结合,多年网站推广经验,合理的价格为您打造企业品质网站。
列表的创建
list_test=[‘张三‘,‘李四’,'alex'] #或 list_test=list('alex') #或 list_test=list([‘张三‘,‘李四’,'alex'])
列表的特点和常用操作
特性:
1.可存放多个值
2.按照从左到右的顺序定义列表元素,下标从0开始顺序访问,有序
3.可修改指定索引位置对应的值,可变
常用操作:
#索引 >>> l = ['egon','alex','seven','yuan'] >>> l[0] 'egon' >>> l[2] 'seven' #切片 >>> l[0:2] ['egon', 'alex'] >>> l[2:5] ['seven', 'yuan'] >>> l[:2] ['egon', 'alex'] >>> l[2:] ['seven', 'yuan'] >>> l[:] ['egon', 'alex', 'seven', 'yuan'] >>> l[::2] ['egon', 'seven'] >>> l[::-1] ['yuan', 'seven', 'alex', 'egon'] #追加 >>> l.append("eva") >>> l ['egon', 'alex', 'seven', 'yuan', 'eva'] #删除 >>> l.remove('eva') >>> l ['egon', 'alex', 'seven', 'yuan'] >>> l.pop() 'yuan' >>> l ['egon', 'alex', 'seven'] #长度 >>> len(l) 3 #包含 >>> 'seven' in l True >>> 'yuan' in l False #循环:为什么是“i”? >>> for i in l: print(i) egon alex seven
列表与字符串——split和join
#分割 >>> s = 'hello world' >>> s.split(' ') ['hello', 'world'] >>> s2= 'hello,world' >>> s2.split(',') #连接 >>> l = ['hi','eva'] >>> '!'.join(l) 'hi!eva'
range
>>> range(1,100) range(1, 100) >>> list(range(1,100)) [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99] >>> list(range(10)) [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] >>> list(range(0,100,2)) [0, 2, 4, 6, 8, 10, 12, 14, 16, 18, 20, 22, 24, 26, 28, 30, 32, 34, 36, 38, 40, 42, 44, 46, 48, 50, 52, 54, 56, 58, 60, 62, 64, 66, 68, 70, 72, 74, 76, 78, 80, 82, 84, 86, 88, 90, 92, 94, 96, 98]
当前名称:python数据类型之列表
本文链接:http://scjbc.cn/article/pgehgo.html