1.列表小练兵
"""
已知列表list1 = ['a','b','c'],使用系统功能完成下面操作
a.在列表的末尾追加一个元素'd'
b.在下标为1的位置插入一个元素'e'
c.统计元素'a'出现的次数
d.将列表元素反转
e.将列表降序排序
f.获取列表长度
"""
list1 = ['a','b','c']
list1.append("d")
list1.extend("d")
list1.insert(1,'e')
list1.count('a')
count = 0
for ele in list1:
if ele == 'a':
count += 1
list1.reverse()
list2 = list1[::-1]
list3 = []
i = len(list1) - 1
while i >= 0:
list3.append(list1[i])
i -= 1
list1.sort(reverse=True)
l = len(list1)
n = 0
for _ in list1:
n += 1
list1 = ["Sun","Mon","Tue","Wed","Thu","Fri","Sat"]
list2 = ["Sun","Mon","Thu","Fri","Sat"]
list3 = []
for ele in list1:
if ele not in list2:
list3.append(ele)
names = ['张三', '李四', '大黄', '张三','张三','张三','张三','张三']
for name in names[:]:
if name == "张三":
names.remove("张三")
print(names)
names = ['张三', '李四', '大黄', '张三','张三','张三','张三','张三']
name = "张三"
count = names.count(name)
for _ in range(count - 1):
names.remove(name)
print(names)
new_names = []
names = ['张三', '李四', '大黄', '张三','张三','张三','张三','张三']
for name in names:
if name not in new_names:
new_names.append(name)
print(new_names)
2.深浅拷贝
"""
copy.copy()表示浅拷贝,最外层的列表会被拷贝一个新的地址,但是
内层列表的地址保持不变,所以当实现copy之后,两个列表的地址不
相同,对于一维列表,一个列表的元素发生改变,另一个列表不会发
生改变,对于二维列表,一个列表的内部元素发生改变,则另一个列
表会随着改变
copy.deepcopy()表示深拷贝,里外层的列表的地址都会被拷贝一个
新的,所以当实现deepcopy之后,两个列表的地址不相同,对于任
意列表,一个列表的内部元素发生改变,则另一个列表不会改变
深浅拷贝(就是在计算机中看是占了一个地址还是占了两个地址的问题)
变量在内存中的地址问题(也就是说的是变量的可变性与不可便行,可变数据类型与不可变类型的)
【problem】可变数据和不可变数据的区别
不可变的数据类型:int,float,bool,str,tuple,通过变量赋值的方式
操作,一个变量的值发生改变,另一个变量不受影响
可变的数据类型:list,dict,set,通过变量赋值的方式操作,一个变
量内部的值发生改变,另一个变量会随着改变
"""
a = 10
b = a
print(a is b)
print(id(a) == id(b))
a = 20
print(a is b)
print(id(a) == id(b))
a = [10,20]
b = a
print(a is b))
print(id(a) == id(b))
a[1] = 100
print(a is b)
print(id(a) == id(b)))
3.列表推导式
- 列表推导式,就是指的轻量级循环创建列表,简单来说,使用一行代码实现简单的逻辑,创建一个新的列表
list11 = list(range(2,11,2))
print(list11)
list12 = []
for i in range(1,6):
list12.append(i * 2)
print(list12)
list13 = [i * 2 for i in range(1,6)]
print(list13)
list21 = []
for i in range(1,6):
list21.append(i ** 2)
print(list21)
list22 = [i ** 2 for i in range(1,6)]
print(list22)
list31 = []
for i in range(1,11):
if i % 2 == 0:
list31.append(i)
print(list31)
list32 = [i for i in range(1,11) if i % 2 == 0]
print(list32)
list33 = [num for num in range(1,51) if num % 6 == 0]
list41 = []
for m in "xyz":
for n in "123":
list41.append(m + n)
print(list41)
list42 = [m + n for m in "xyz" for n in "123"]
print(list42)
list51 = []
for n in range(1,101):
if n % 3 == 0 and n % 6 == 0:
list51.append(n)
print(list51)
list51 = [n for n in range(1,101) if n % 3 == 0 and n % 6 == 0]
print(list51)
list51 = []
for n in range(1,101):
if n % 3 == 0:
if n % 6 == 0:
list51.append(n)
print(list51)
list51 = [n for n in range(1,101) if n % 3 == 0 if n % 6 == 0]
print(list51)
"""
注意:在列表推导式中,for循环和if语句都可以根据具体的需求书写多个,
但是,多个for或者多个if之间的关系从左往右是嵌套的关系
"""
list1 = ["abc",34,65,6,"hello",35,"fff"]
list61 = [ele for ele in list1 if type(ele) == int]
print(list61)
list62 = [ele for ele in list1 if isinstance(ele,int)]
print(list62)
list71 = [str(i) + "-" + str(i + 1) for i in range(1,5)]
print(list71)
list8 = ['a',"B","C","H",'n','m',"45"]
list81 = []
for ch in list8:
if ch >= "a" and ch <= "z":
list81.append(chr(ord(ch) - 32))
elif ch >= "A" and ch <= "Z":
list81.append(chr(ord(ch) + 32))
print(list81)
list81 = []
for ch in list8:
if "a" <= ch <= "z":
list81.append(chr(ord(ch) - 32))
elif "A" <= ch <= "Z":
list81.append(chr(ord(ch) + 32))
print(list81)
list82 = []
for ch in list8:
if ch.islower():
list82.append(ch.upper())
elif ch.isupper():
list82.append(ch.lower())
print(list82)
"""
列表推导式:
a.列表推导式,字典推导式以及三目运算符这些使用一行代码实现逻辑的操作并不是所有的场景都适用
b.只适用于简单的逻辑
c.如果逻辑复杂,则代码可读性会降低,代码的可维护性会降低
"""
4.算法 4.1排序算法
numlist = [34,6,5,6,100,55,89,16]
max_value = numlist[0]
for num in numlist:
if num > max_value:
max_value = num
print(max_value)
numlist = [34,6,5,6,100,55,89,16]
max_value = numlist[0]
for i in range(1,len(numlist)):
if numlist[i] > max_value:
max_value = numlist[i]
print(max_value)
numlist = [34,6,5,6,100,55,100,89,100,16]
new_list = numlist.copy()
max_value = numlist[0]
for num in numlist:
if num > max_value:
max_value = num
print(max_value)
max_count = numlist.count(max_value)
for _ in range(max_count):
numlist.remove(max_value)
print(numlist)
second_value = numlist[0]
for num in numlist:
if num > second_value:
second_value = num
for i in range(len(new_list)):
if new_list[i] == second_value:
print(second_value,i)
numlist = [34,6,5,6,100,55,100,89,100,16]
new_list = numlist.copy()
numlist.sort()
print(numlist)
max_value = numlist[-1]
second_value = numlist[-(numlist.count(max_value) + 1)]
for i in range(len(new_list)):
if new_list[i] == second_value:
print(second_value,i)
"""
#冒泡排序
排序思路:比较两个相邻的下标对应的元素,如果符合条件就交换位置(最值出现在最后位)【自己添加: 核心:两个相邻的下标】
使用嵌套循环:for循环
外层循环控制比较的轮数,内层循环控制循每一轮参与比较的下标
外层循环范围:0--len-2 range(len-1)#因为第len-1个索引娶不到
内层循环:0--len()-2-i range(0,len-1-i)
"""
list1 = [34,100,45,28,6,499,47,5]
for i in range(len(list1) - 1):
for j in range(len(list1) - 1 - i):
if list1[j] > list1[j + 1]:
list1[j],list1[j + 1] = list1[j + 1],list1[j]
print(list1)
"""
【插入排序】:
#思路:
以升序为例:
1.新列表中没有元素就直接添加
2.先判断当前数值与新列表中的最后一个元素以及第一个元素的大小
小于第一个元素直接插入到最前面
大于最后一个元素直接插入到最后面
3.遍历:从前往后比较:前一个《= 当前 《= 后一个
"""
numlist = [1,4,5,34,6,5,6,100,55,8,200,1]
numlist1 = []
while len(numlist):
num = numlist.pop(-1)
if not len(numlist1):
numlist1.append(num)
elif num >= numlist1[-1]:
numlist1.append(num)
elif num <= numlist1[0]:
numlist1.insert(0,num)
else:
for i in range(len(numlist1)-1):
if numlist1[i] <= num <=numlist1[i+1]:
numlist1.insert(i+1,num)
break
print(numlist1)
"""
排序思路:固定一个下标,然后拿这个下标对应的值依次和后面的元素进行比较,最值出现在头角标位置上
"""
list1 = [34,100,45,28,6,499,47,5]
for i in range(len(list1) - 1):
for j in range(i + 1,len(list1)):
if list1[i] > list1[j]:
list1[i],list1[j] = list1[j],list1[i]
print(list1)
5.查找
numlist = [34,6,5,6,100,55,100,89,100,16]
key = 100
for i in range(len(numlist)):
if key == numlist[i]:
print("%d元素的下标为:%d" % (key,i))
print(numlist.index(100))
key = 100
for i in range(len(numlist)):
if key == numlist[i]:
print("%d元素的下标为:%d" % (key,i))
break
"""
#二分法查找
局限性:你的列表必须是有序的,升序或者降序
查找思路:升序的前提下,将待查找的元素与中间下标对应的元素比较,如果待查找元素大于中间下标对应的元素,则去右半部分查找
注意:前提是列表是有序(升序或者降序)的,通过折半来缩小查找范围,提高查找效率
"""
list1 = [34,100,45,28,6,100,100,499,47,5]
for i in range(len(list1) - 1):
for j in range(len(list1) - 1 - i):
if list1[j] > list1[j + 1]:
list1[j],list1[j + 1] = list1[j + 1],list1[j]
print(list1)
key = 66
left = 0
right = len(list1) - 1
while left <= right:
middle = (left + right) // 2
if key > list1[middle]:
left = middle + 1
elif key < list1[middle]:
right = middle - 1
else:
print("待查找元素%d的下标为:%d" % (key, middle))
break
else:
print("待查找元素%d在列表中不存在" % (key))
6.元组
-
列表的本质:列表是一种有序的,可变的,可以存储重复元素的集合 -
元组的本质:元组是一种有序的,不可变的,可以存储重复元素的集合 -
元组和列表的不同之处: a.列表:[] 元组:() b.列表中的元素可以进行增加和删除操作,但是,元组中的元素不能修改【元素:一旦被初始化,将不能发生改变】
创建列表:
创建空列表:list1 = []
创建有元素的列表:list1 = [元素1,元素2,......]
创建元组
创建空元组:tuple1 = ()
创建有的元组:tuple1 = (元素1,元素2,.......)
list1 = [3,5,6]
tuple1 = (3,5,6)
print(type(list1),type(tuple1))
l1 = ["aaa",True,10]
t1 = ("aaa",True,10)
print(l1,t1)
l2 = [4,4,4,4,6]
t2 = (4,4,4,4,6)
print(l2,t2)
l3 = [10]
t2 = (9)
t3 = (10,)
print(l3,t3)
l1 = ["aaa",True,10]
t1 = ("aaa",True,10)
print(l1[1],t1[1])
l1[1] = 100
print(l1)
l1 = ["aaa",True,10]
t1 = ("aaa",True,10)
del l1[1]
t1 = ("aaa",True,10)
print(type(t1))
l1 = list(t1)
print(type(l1))
l1 = ["aaa",True,10]
print(type(l1))
t1 = tuple(l1)
print(type(t1))
t1 = (1,2,3)
t2 = (4,5,6)
print(t1 + t2)
print(t1 * 4)
print(2 in t1)
print(10 not in t1)
print(len(t1))
print(max(t1))
print(min(t1))
print(t1.count(2))
tuple1 = (34,4,6,56,5)
n = 0
while n < len(tuple1):
print(tuple1[n])
n += 1
for num in tuple1:
print(num)
for i in range(len(tuple1)):
print(i,tuple1[i])
for i,ele in enumerate(tuple1):
print(i,ele)
t1 = (4,5,6,[11,22])
print(t1)
t1[-1][1] = 100
print(t1)
"""
【problem】:元组的意义:涉及到进程和线程,多进程多线程中有的不希望被改掉,那么就可以用元组
【problem】元组和列表的区别和联系
相同点:
a.二者都是有序的
b.二者都可以存储不同类型的元素
c.二者都可以存储重复元素
d.二者的遍历方式完全相同
不同点:
a.表示方式,元组:(),列表:[]
b.可变性:元组是不可变的,列表是可变的
c.使用场景:在多线程的使用场景中,为了避免多线程访问同一个数据造成数据的混乱,则可以使用元组,
如果涉及元素的增加或者删除,则可以使用列表
d.元组在创建时间上和占用的空间上优于列表
{注意:列表的底层维护了一栈,计算机底层快速的复制了一个然后进行增加等操作,操作一次复制一次,因为元组不可变,所以,元组不需要复制}
"""
7.字典
-
列表和元组的使用缺点:当存储的数据要动态添加、删除的时候,我们一般使用列表,但是列表有时会遇到一些麻烦 -
解决方案:既能存储多个数据,还能在访问元素的很方便的定位到需要的元素,采用字典 -
字典的本质:字典是一种无序的,可变的,存储键值对的集合 习惯使用场景: -
列表更适合保存相似数据,比如多个商品、多个姓名、多个时间 -
字典更适合保存不同数据,比如一个商品的不同信息、一个人的不同信息
"""
#语法
语法:{键1:值1, 键2:值2, 键3:值3, ..., 键n:值n}
说明:
- 字典和列表类似,都可以用来存储多个数据
- 在列表中查找某个元素时,是根据下标进行的;字典中找某个元素时,是根据'名字'(就是冒号:前面的那个值,例如上面代码中的'name'、'id'、'sex')
- 字典中的每个元素都由2部分组成,键:值。例如 'name':'班长' ,'name'为键,'班长'为值
- 键可以使用数字、布尔值、元组,字符串等不可变数据类型,但是一般习惯使用字符串,切记不能使用列表,字典(字典数据类型本身也是可变的)等可变数据类型
- 每个字典里的key都是唯一的,如果出现了多个相同的key,后面的value会覆盖之前的value
"""
dict1 = {}
print(type(dict1))
dict2 = {"zhangsan":99,"lisi":100,"zhangsan":66}
print(dict2)
dict3 = {"zhangsan":99,"lisi":100,"tom":55,"bob":19}
print(dict3)
print(dict3["lisi"])
dict3["lisi"] = 45
print(dict3)
dict3["rose"] = 77
print(dict3)
dict1 = {"aa":10,"bb":20}
print(dict1)
dict2 = {}
dict2["aa"] = 10
dict2["cc"] = 30
print(dict2)
dict3 = dict(name='hello',age=10)
print(dict3)
dict4 = dict([("a",11),('b',22),('c',33)])
print(dict4)
dict5 = dict(zip([11,22],['a','b','c']))
print(dict5)
dict1 = dict.fromkeys([11,22])
print(dict1)
dict1 = dict.fromkeys((11,22))
print(dict1)
dict1 = {"zhangsan":99,"lisi":100,"tom":55,"bob":19}
print(dict1.keys())
print(dict1.values())
print(dict1.items())
for key in dict1:
print(key,dict1[key])
for key in dict1.keys():
print(key,dict1[key])
for value in dict1.values():
print(value)
for key,value in dict1.items():
print(key,value)
8.大练兵
"""
生成50-300之间可重复的10个数据存放于列表中, 对列表进行排重,
并对列表使用冒泡排序进行降序排序
例如:随机生成了[70, 88, 91, 70, 107, 234, 91, 177, 282, 197]
去重之后 [70, 88, 91, 107, 234, 177, 282, 197]
降序排序 [282, 234, 197, 177, 107, 91, 88, 70]
"""
import random
nums = []
for _ in range(10):
nums.append(random.choice(range(50,301)))
print(nums)
for i in range(len(nums)-1):
count = nums.count(nums[i])
if count >= 2:
nums.remove(nums[i])
print(nums)
for m in range(len(nums)):
for n in range(len(nums)-1-m):
if nums[n] < nums[n+1]:
nums[n],nums[n+1] = nums[n+1],nums[n]
print(nums)
import random
list1 = [random.randrange(50,300) for i in range(0,10) ]
for num in list1:
while list1.count(num) > 1:
list1.remove(num)
print(list1)
for i in range(len(list1)-1):
for j in range(len(list1)-1-i):
if list1[j] < list1[j+1]:
list1[j],list1[j+1] = list1[j+1],list1[j]
print(list1)
"""
[运行结果]
[288, 207, 246, 87, 221, 50, 236, 242, 273, 79]
[288, 273, 246, 242, 236, 221, 207, 87, 79, 50]
"""
import random
num_list = []
for _ in range(10):
num_list.append(random.choice(50,301))
list1 = []
for num in num_list:
if num not in list1:
list1.append(num)
for i in range(len(list1) - 1):
for j in range(len(list1) - 1 - i):
if list1[j] < list1[j + 1]:
list1[j],list1[j + 1] = list1[j + 1],list1[j]
"""
2.自定义一个数字列表,获取这个列表中的最小值,并将列表转化为
元组,要求:不能使用min()
"""
list2 = [12,31,42,52,14,63,52,88]
list2.sort()
min_value = list2[0]
tuple2 = tuple(list2)
print(min_value)
print(tuple2)
num_list = [34,6,546,5,100,16,77,56,88,10]
min_value = num_list[0]
for i in range(1,len(num_list)):
if num_list[i] < min_value:
min_value = num_list[i]
num_tuple = tuple(num_list)
"""
3.自定义一个数字列表,元素为10个,找出列表中最大数连同下标一起
输出
"""
num_list = [34,6,546,5,100,16,77,56,88,10]
max_value = num_list[0]
max_index = 0
for i in range(1,len(num_list)):
if num_list[i] > max_value:
max_value = num_list[i]
max_index = i
print("最大值%d在列表中的位置为:%d" % (max_value,max_index))
list3 = [14,52,25,82,14,52,15,77,32,71]
max_value = list3[0]
for i in range(len(list3)):
if max_value < list3[i]:
max_value = list3[i]
for j in range(len(list3)):
if max_value == list3[j]:
num = j
print('列表中最大数%s,%s'%(max_value,num))
list3 = [131, 262, 83, 271, 228, 243, 170, 200, 188, 178]
MaxNum = max(list3)
print(MaxNum)
index = list3.index(MaxNum)
print(index)
"""
运行结果: 271 3
"""
"""
4.利用列表推导式, 完成以下需求
"""
list1 = [i for i in range(100+1) if i%100%10 == 3]
print(list1)
list1 = [i for i in range(1, 101)if i % 10 == 3]
list1 = [True, 17, "hello", "bye", 98, 34, 21]
list2 = [i for i in list1 if isinstance(i, int) and not isinstance(i,bool)]
list42 = [True, 17, "hello", "bye", 98, 34, 21]
list421 = [num for num in list2 if type(num) == int]
print(list421)
list43 = ["good", "nice", "see you", "bye"]
list431 = [len(i) for i in list43 ]
print(list431)
list2 = ["good", "nice", "see you", "bye"]
list22=[len(list2[i])for i in range(len(list2)) ]
print(list22)
list1 = ["good", "nice", "see you", "bye"]
list2 = [len(i) for i in list1]
"""
1.自定义一个数字列表,求列表中第二大数的下标
"""
list1 = [14,52,25,82,14,52,15,77,32,71]
for i in range(len(list1)):
for j in range(i,len(list1)):
if list1[i] < list1[j]:
list1[i],list1[j] = list1[j],list1[i]
max_count = list1.count(list1[0])
for m in range(len(list1)):
if m > max_count:
print('列表中第二大数%s的下标:%s'%(list1[m],m))
break
list1 = [14,52,25,82,14,52,15,77,32,71]
tuple1 = tuple(list1)
list1.sort(reverse=False)
print(tuple1.index(list1[-2]))
num_list = [34,6,546,5,100,546,546,16,77]
new_list = num_list.copy()
for i in range(len(new_list) - 1):
for j in range(len(new_list) - 1 - i):
if new_list[j] > new_list[j + 1]:
new_list[j],new_list[j + 1] = new_list[j + 1],new_list[j]
print(new_list)
max_value = new_list[-1]
max_count = new_list.count(max_value)
second_value = new_list[-(max_count + 1)]
for i in range(len(num_list)):
if second_value == num_list[i]:
print("第二大值%d在列表中的下标为:%d" % (second_value,i))
"""
2.已知列表list = [34,55,67,88,99,100],使用二分法查找67在列表中的位置
"""
list2 = [34,55,67,88,99,100]
key = 67
left = 0
right = len(list2)-1
while left <= right:
middle = (left + right) // 2
if key < list2[middle]:
right += 1
elif key > list2[middle]:
right += 1
else:
print('%s在列表中的位置:%s'%(key,middle))
break
list1 = [34,55,67,88,99,100]
list1.sort()
key = 67
left = 0
right = len(list1) - 1
while left <= right:
middle = (left + right) // 2
if list1[middle] < key:
left = middle + 1
elif list1[middle] > key:
right = middle - 1
else:
print("%d索引为:%d" % (key,middle))
break
else:
print("%d在列表中不存在" % (key))
"""
3.自定义一个列表,最大的与第一个元素交换,最小的与最后一个元素交换,输出交换之后的列表
"""
list3 = [14,52,25,82,57,52,6,77,32,71]
list31 = list3.copy()
list31.sort()
max_value = list31[-1]
min_value = list31[0]
for i in range(len(list3)):
if max_value == list3[i]:
break
for j in range(len(list3)):
if min_value == list3[j]:
break
list3[0],list3[i] = list3[i],list3[0]
list3[-1],list3[j] = list3[j],list3[-1]
print(list3)
list_num = [1332, 45, 43243, 4346, 34234, 656, 323, 99999]
maxi = list_num[0]
minn = list_num[0]
for i in range(1, len(list_num)):
if list_num[i] > maxi:
maxi = list_num[i]
for i in range(1, len(list_num)):
if list_num[i] < minn:
minn = list_num[i]
list_num[0], maxi = maxi, list_num[0]
list_num[-1], minn = minn, list_num[-1]
print(list_num)
"""
4.从控制台输入一段文本, 统计每个字符出现的次数
"""
text = input('输入一段文本')
list41 = []
list41.extend(text)
list42 = []
for i in range(len(list41)):
count = list41.count(list41[i])
list42.append(count)
print(list41)
print(list42)
dict1 = dict(zip(list41,list42))
print(dict1)
eng = input("输入一个英文句子:")
dict = {}
for i in eng:
if i not in dict:
dict[i] = 1
else:
dict[i] += 1
print(dict)
"""
1.dict_list = [{“科目”:“政治”, “成绩”:98}, {“科目”:“语文”, “成绩”:77}, {“科目”:“数学”, “成绩”:99}, {“科目”:“历史”, “成绩”:65}]
按照字典中的成绩对列表进行降序排序 【使用排序算法】
结果为: [{“科目”:“数学”, “成绩”:99}, {“科目”:“政治”, “成绩”:98}, {“科目”:“语文”, “成绩”:77}, {“科目”:“历史”, “成绩”:65}]
"""
dict_list = [{'科目':'政治', '成绩':98}, {'科目':'语文', '成绩':77}, {'科目':'数学', '成绩':99}, {'科目':'历史', '成绩':65}]
for i in range(len(dict_list)):
for j in range(i,len(dict_list)):
if dict_list[i]['成绩'] < dict_list[j]['成绩']:
dict_list[i],dict_list[j]=dict_list[j],dict_list[i]
print(dict_list)
dict_list = [
{'科目':'政治', '成绩':98},
{'科目':'语文', '成绩':77},
{'科目':'数学', '成绩':99},
{'科目':'历史', '成绩':65}
]
newDictList = sorted(dict_list,key=lambda x:x["成绩"],reverse=True)
print(newDictList)
"""
newDictList = sorted(dict_list,key=lambda x:x["成绩"],reverse=True)
在匿名函数当中我们接收的x是kids当中的元素,也就是一个dict,
所以我们想要指定我们希望的字段,需要用dict访问元素的方法,
也就是用中括号来查找对应字段的值。
学习自定义排序:
链接:https://blog.csdn.net/weixin_39949506/article/details/110701950?utm_term=python%E8%87%AA%E5%AE%9A%E4%B9%89%E6%8E%92%E5%BA%8F%E8%A7%84%E5%88%99&utm_medium=distribute.pc_aggpage_search_result.none-task-blog-2~all~sobaiduweb~default-2-110701950&spm=3001.4430
"""
dict_list = [
{'科目':'政治', '成绩':98},
{'科目':'语文', '成绩':77},
{'科目':'数学', '成绩':99},
{'科目':'历史', '成绩':65}
]
for i in range(len(dict_list) - 1):
for j in range(len(dict_list) - 1 - i):
if dict_list[j]["成绩"] < dict_list[j + 1]["成绩"]:
dict_list[j],dict_list[j + 1] = dict_list[j + 1],dict_list[j]
print(dict_list)
"""
2.dict_list = [{“科目”:“政治”, “成绩”:98}, {“科目”:“语文”, “成绩”:77}, {“科目”:“数学”, “成绩”:99}, {“科目”:“历史”, “成绩”:65}]
去除列表中成绩小于70的字典 【列表推导式完成】
"""
dict_list = [
{'科目':'政治', '成绩':98},
{'科目':'语文', '成绩':77},
{'科目':'数学', '成绩':99},
{'科目':'历史', '成绩':65}
]
newlist = [subdict for subdict in dict_list if subdict["成绩"] >= 70]
print(newlist)
|