特点: 是一个有序的,可以重复的序列,可变类型
1.创建一个空列表。
list1 = [] print(type(list1))2.创建一个有多个值的列表,每个值用逗号隔开。
lit1 = [18, 25, 16, 22, 28]列表也可以存放不同类型的元素。
lst2 = [18, "str", [1, 2, 3], (66, 67), {'name':'Tom', 'age':18}]列表:
names = ['a','b','c','d']append(),extend(),insert()
1)append(),增加到最后 >>> names = ['a','b','c','d'] >>> names.append('e') >>> names ['a', 'b', 'c', 'd', 'e'] >>> names = ['a','b','c','d'] >>> names.append([1, 2, 3]) >>> names ['a', 'b', 'c', 'd', [1, 2, 3]] 2)extend(),迭代的去增 >>> names = ['a', 'b', 'c', 'd'] >>> place = ['beijing', 'shandong', 'usa'] >>> names.extend(place) >>> names ['a', 'b', 'c', 'd', 'beijing', 'shandong', 'usa'] >>> 3)insert(),按照索引去增加 names = ['a', 'b', 'c', 'd'] names.insert(2,'devilf') names ['a', 'b', 'devilf', 'c', 'd'] 4)其他的插入方法: >>> names[3] = 'lebron' >>> names ['a', 'b', 'devilf', 'lebron', 'd']pop,remove,del,clear
1)pop() >>> names = ['a', 'b', 'c', 'd', 'e'] >>> names.pop() # 如果没有指定下标,则默认会删除最后一个元素 'e' >>> names ['a', 'b', 'c', 'd'] >>> names.pop(2) # 指定索引时,就会删除索引所对应的元素 'c' >>> names ['a', 'b', 'd'] >>> 2)remove(),按照元素删除 >>> names = ['a', 'b', 'c', 'd', 'e', 'f', 'e'] >>> names.remove('e') #移除第一次遇到的指定元素 >>> names ['a', 'b', 'c', 'd', 'f', 'e'] >>> 3)del >>> names = ['a', 'b', 'c', 'd', 'e', 'f', 'e'] >>> del names[4] >>> names ['a', 'b', 'c', 'd', 'f', 'e'] >>> 4)clear() >>> names = ['a', 'b', 'c', 'd', 'e', 'f', 'e'] >>> names.clear() >>> names []切片去查,或者,循环去查
索引、切片、遍历1.查找元素所在位置:index()
>>> names = ['a', 'b', 'c', 'd'] >>> names.index('c') 2 遍历3种:
(1)元素遍历
>>> li = ["python", "Java", "C++"] >>> for i in li: ... print(i, end = " ") ... python Java C++(2)索引遍历
>>> li = ["python", "Java", "C++"] >>> for i in range(len(li)): ... print(li[i], end = " ") ... python Java C++(3)枚举遍历
枚举(enumerate),对于一个可迭代的(iterable)/可遍历的对象(如列表、字符串),enumerate将其组成一个索引序列,利用它可以同时获得索引和值。
>>> li = ['a', 'b', 'c', 'd'] >>> for i in enumerate(li): ... print(i) ... (0, 'a') (1, 'b') (2, 'c') (3, 'd') >>> li ['a', 'b', 'c', 'd'] >>> for index, name in enumerate(li, 1): ... print(index, name) ... 1 a 2 b 3 c 4 d >>> for index, name in enumerate(li, 10): ... print(index, name) ... 10 a 11 b 12 c 13 d >>>1.统计元素的次数:count()
>>> names = ['a', 'b', 'c', 'd'] >>> names.append('d') >>> names.count('d') 22.反转:reverse()
>>> names = ['a', 'b', 'c', 'd'] >>> names.reverse() >>> names ['d', 'c', 'b', 'a']3.排序:sort()按照ascii码来进行排序
>>> names = ['a', 'b', 'c', 'd'] >>> names.insert(3,'&&') >>> names ['a', 'b', 'c', '&&', 'd'] >>> names.sort() >>> names ['&&', 'a', 'b', 'c', 'd'] >>> names.sort(reverse=True) >>> names ['d', 'c', 'b', 'a', '&&'] >>> names[::-1] ['&&', 'a', 'b', 'c', 'd']4.练习题
1)列出所有的元素 names = ['&&', 'a', 'b', 'd', 'devilf', 'lebron', 'beijing', 'shandong', 'usa'] >>> names[::] ['&&', 'a', 'b', 'd', 'devilf', 'lebron', 'beijing', 'shandong', 'usa'] 2)列出最后一个元素,从中间位置开始,列出后面所有的元素 >>> names[-1] 'usa' >>> a = int(len(names)/2) >>> names[a:] ['devilf', 'lebron', 'beijing', 'shandong', 'usa']11.复制:copy()
>>> names.copy() ['&&', 'a', 'b', 'd', 'devilf', 'lebron', 'beijing', 'shandong', 'usa']6.另外的几种复制的方法:
>>> info = ['name',['a',100]] >>> n1 = copy.copy(info) >>> n2 = info[:] >>> n3 = list(info)在使用copy.copy()时,需要导入copy模块
这些均是浅copy
例如:
>>> info ['name', ['a', 100]] >>> n1 = info[:] >>> n2 = copy.copy(info) >>> n1 ['name', ['a', 100]] >>> n1[0] = 'devilf' >>> n2[0] = 'lebron' >>> n1;n2 ['devilf', ['a', 100]] ['lebron', ['a', 100]] >>> n1[1][1] = 80 >>> n1 ['devilf', ['a', 80]] >>> n2 ['lebron', ['a', 80]]这里可以看到修改n1列表中的值,n2中的值也会跟着改变,这就是浅copy,也就是说,浅copy会复制原列表的内存地址,也就是说,我们修改了n1和n2,就是修改了指向同一内存地址的对象,所以info列表会变化,n1和n2都会变化,例如:
>>> info ['name', ['a', 80]]生出30个0~10的整数,统计每个数字生成的次数,输出一个列表。
import random count=0 list1=[] list2=[] for i in range(30): num=random.randint(0,10) list1.append(num) sum=0 for j in range(11): x=list1.count(j) list2.append([j,x]) sum=sum+x print(list2) #[[0, 5], [1, 2], [2, 0], [3, 4], [4, 4], [5, 3], [6, 0], [7, 5], [8, 2], [9, 2], [10, 3]] print(sum) #30