您好,登录后才能下订单哦!
密码登录
登录注册
点击 登录注册 即表示同意《亿速云用户服务条款》
定义:[]内以逗号分隔,按照索引,存放各种数据类型,每个位置代表一个元素
列表的创建
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
#分割 >>> s = 'hello world' >>> s.split(' ') ['hello', 'world'] >>> s2= 'hello,world' >>> s2.split(',') #连接 >>> l = ['hi','eva'] >>> '!'.join(l) 'hi!eva'
>>> 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]
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。