1.很干的干货
1.列表不能再循环的时候删除. 因为索引会跟着改变,字典也不能直接循环删除.把要删除的内容记录在列表中. 循环列表. 删除原列表, 字典中的数据
fromkeys() 不会对原来的字典产生影响. 产生新字典(神坑, 考试)
set集合. 不重复, 无序.
2.关于深浅拷贝几个案例
2.1 a和b赋值(b=2)
a=2
b=a
a=3
print(b) #b=2
2.2 列表中的拷贝
a=[1,2,3,4,5,6]
b=a
index_a=a.index(6) #5 查找第一个6在列表的索引位置
a[index_a]='hello,world'
print(a) #[1, 2, 3, 4, 5, 'hello,world']
print(b) #[1, 2, 3, 4, 5, 'hello,world']
2.3 列表中的拷贝二
a=[1,2,3,4,5,6,['wangsiyu',666,(5,)]]
b=a
a[6][0]='hello'
print(a) #[1, 2, 3, 4, 5, 6, ['hello', 666, (5,)]]
print(b) #[1, 2, 3, 4, 5, 6, ['hello', 666, (5,)]]
3.练习题
3.1 把可口可乐插入到abc:
a='可口可乐'
b=a.join('123')
print(b) #1可口可乐2可口可乐3
3.2 把列表转为字符串,把字符串转为列表
a=['hello','wangsiyu','wangpei','world']
#把列表转为字符串
b=' '.join(a)
print(b) #hello wangsiyu wangpei world
#把字符串转为列表
c=b.split(' ')
print(c) #['hello', 'wangsiyu', 'wangpei', 'world']
3.3 删除列表姓周的人,注意,不能再原列表删除,因为索引会变化,把要删除的记录下来
a=['周星驰','周杰伦','王思宇','周伯通']
del_a=[]
for i in a :
if i[0]=='周':
del_a.append(i)
#此时已经拿到要删除的列表了。
for i in del_a:
a.remove(i)
print(a) #['王思宇']
3.4 dict.fromkeys()鬼东西,不知道有什么用,写上吧
res=dict.fromkeys('王思宇','钱')
print(res) #{'王': '钱', '思': '钱', '宇': '钱'}
3.5 列表的去重
lst=['王思雨','哪吒','悟空','大雨','牛魔王','王思雨','王凯']
a=set(lst)
b=list(a)
print(a) #{'悟空', '牛魔王', '王思雨', '大雨', '哪吒', '王凯'}
3.6 浅COPY创建对象
lst1=[1,2,3,4,5,6]
lst2=lst1.copy()
print(lst1,lst2) #[1, 2, 3, 4, 5, 6] [1, 2, 3, 4, 5, 6]
print(id(lst1),id(lst2)) #9979344 9980504 两个虽然相等,但不是同一个
lst2.append('王思宇')
print(lst1,lst2) #[1, 2, 3, 4, 5, 6] [1, 2, 3, 4, 5, 6, '王思宇']
3.7浅copy遇到的问题
lst1=[1,2,3,4,5,6,['哪吒','悟空']]
lst2=lst1.copy()
lst1[6].append('葫芦')
print(lst1,lst2) #[1, 2, 3, 4, 5, 6, ['哪吒', '悟空', '葫芦']] [1, 2, 3, 4, 5, 6, ['哪吒', '悟空', '葫芦']]
3.8 深拷贝
import copy
lst1=[1,2,3,4,5,6,['哪吒','悟空']]
lst2=copy.deepcopy(lst1)
lst1[6].append('葫芦')
print(lst1,lst2) #[1, 2, 3, 4, 5, 6, ['哪吒', '悟空', '葫芦']] [1, 2, 3, 4, 5, 6, ['哪吒', '悟空']]
3.9 十个老师轮流打分,分数必须在5-10之间,并打印老师给的分数
i=1
dict={}
while i <=10:
msg=int(input('第%d个老师给的分数是:'%i))
if msg>5 and msg <10:
dict[i]=msg
else:
print('成绩范围必须在5-10之间')
continue
i+=1
print(dict)
3.10 循环列表movie=['西游记','水浒传','三国杀','牛博网','葫芦娃'],并把字典打印出来
movie=['西游记','水浒传','三国杀','牛博网','葫芦娃']
dict={}
for i in movie:
print(i)
score=input('请输入分数:')
dict[i]=score
print(dict)
3.11 输入数字,打印数字的发音
dict={'1':'yi','2':'er','3':'san','4':'si','5':'wu','6':'liu','7':'qi','8':'ba','9':'jiu'}
content=input('请输入数字:')
for i in content:
print(dict[i],end='--')
3.12 现在有一群主播:actor={'mayun':12000,'mahuateng':8790,'leijun':9873,'zunzhengyi':5639,'liyanhong':9999}
需求1:算主播平均工资
需求2:干掉收益小于平均值的主播
需求3:干掉马云
代码块
# > 需求1:算主播平均工资
dict={'mayun':12000,'mahuateng':8790,'leijun':9873,'zunzhengyi':5639,'liyanhong':9999}
sum=0
for i in dict.values():
sum +=i
print(sum/len(dict)) #9260.2
代码块
# 需求2:干掉收益小于平均值的主播(字典在遍历过程不能进行删除操作)
dict={'mayun':12000,'mahuateng':8790,'leijun':9873,'zunzhengyi':5639,'liyanhong':9999}
del_dict={}
sum=0
for i in dict.values():
sum +=i
avg_money=(sum/len(dict)) #9260.2
for k,v in dict.items():
if v < avg_money:
del_dict[k]=v
for k in del_dict.keys():
del dict[k]
print(dict) #{'mayun': 12000, 'leijun': 9873, 'liyanhong': 9999}
# 需求3:干掉马云
dict={'mayun':12000,'mahuateng':8790,'leijun':9873,'zunzhengyi':5639,'liyanhong':9999}
dict.pop('mayun')
print(dict)
别跑,点个赞再走