列表

it2022-05-05  141

列表

特点: 是一个有序的,可以重复的序列,可变类型

1.创建一个空列表。

list1 = [] print(type(list1))

2.创建一个有多个值的列表,每个值用逗号隔开。

lit1 = [18, 25, 16, 22, 28]

列表也可以存放不同类型的元素。

lst2 = [18, "str", [1, 2, 3], (66, 67), {'name':'Tom', 'age':18}]

1,列表的拼接

lst1 + lst2

2,列表的重复

lst1 * 3

3,列表索引和切片

lst2 = [18, "str", [1, 2, 3], (66, 67), {'name':'Tom', 'age':18}] len(lst2) lst2[0] 18 lst2[3] (66, 67) lst2[-1] {'name': 'Tom', 'age': 18} lst2[3:] [(66, 67), {'name': 'Tom', 'age': 18}] lst2[0::2] [18, [1, 2, 3], {'name': 'Tom', 'age': 18}]

4,列表的常见操作

列表:

names = ['a','b','c','d']

1.增加

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']

2.删除

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 []

3.改

>>> li = ['a', 'b', 'c', 'd'] >>> li[1] = 'cc' >>> li ['a', 'cc', 'c', 'd'] >>> li[0:2] = ['aa', 'bb'] >>> li ['aa', 'bb', 'c', 'd'] >>>

4.查

切片去查,或者,循环去查

索引、切片、遍历

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 >>>

5.其他操作:

1.统计元素的次数:count()

>>> names = ['a', 'b', 'c', 'd'] >>> names.append('d') >>> names.count('d') 2

2.反转: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

6.列表生成式

列表生成式,用来生成列表的特定语法形式 sr='城市学院' print([ord(x) for x in sr]) #[22478, 24066, 23398, 38498] print([x for x in range(10) if x%2==0]) #[0, 2, 4, 6, 8] 格式: [表达式 for 迭代对象 in 可迭代对象] 实现原理 - 迭代[可迭代对象]中的每一个元素 - 每迭代一次的结果复制给对应的迭代元素,在通过表达式运算得到一个新的值 - 最后所有通过表达式计算的值以一个列表的形式返回。 #三目运算符 y = 10 x = y+1 if y>10 else y-1 print(x) #9 #带过滤功能格式 #[exp for iter_val in iter if if_condtion] print([x for x in range(10) if x%2==0]) #[0, 2, 4, 6, 8] name = ['a','b','c'] subject=['p','j','c','c++'] print([[i,j] for i in name for j in subject]) #[['a', 'p'], ['a', 'j'], ['a', 'c'], ['a', 'c++'], ['b', 'p'], ['b', 'j'], ['b', 'c'], ['b', 'c++'], ['c', 'p'], ['c', 'j'], ['c', 'c'], ['c', 'c++']]

最新回复(0)