您好,登錄后才能下訂單哦!
定義:[]內以逗號分隔,按照索引,存放各種數據類型,每個位置代表一個元素
列表的創建
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進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。