1.很干的干貨
1.列表不能再循環(huán)的時候刪除. 因為索引會跟著改變戈鲁,字典也不能直接循環(huán)刪除.把要刪除的內(nèi)容記錄在列表中. 循環(huán)列表. 刪除原列表, 字典中的數(shù)據(jù)
fromkeys() 不會對原來的字典產(chǎn)生影響. 產(chǎn)生新字典(神坑, 考試)
set集合. 不重復(fù), 無序.
2.關(guān)于深淺拷貝幾個案例
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.練習(xí)題
3.1 把可口可樂插入到abc:
a='可口可樂'
b=a.join('123')
print(b) #1可口可樂2可口可樂3
3.2 把列表轉(zhuǎn)為字符串,把字符串轉(zhuǎn)為列表
a=['hello','wangsiyu','wangpei','world']
#把列表轉(zhuǎn)為字符串
b=' '.join(a)
print(b) #hello wangsiyu wangpei world
#把字符串轉(zhuǎn)為列表
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)
#此時已經(jīng)拿到要刪除的列表了足删。
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創(chuàng)建對象
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 十個老師輪流打分很魂,分數(shù)必須在5-10之間扎酷,并打印老師給的分數(shù)
i=1
dict={}
while i <=10:
msg=int(input('第%d個老師給的分數(shù)是:'%i))
if msg>5 and msg <10:
dict[i]=msg
else:
print('成績范圍必須在5-10之間')
continue
i+=1
print(dict)
3.10 循環(huán)列表movie=['西游記','水滸傳','三國殺','牛博網(wǎng)','葫蘆娃'],并把字典打印出來
movie=['西游記','水滸傳','三國殺','牛博網(wǎng)','葫蘆娃']
dict={}
for i in movie:
print(i)
score=input('請輸入分數(shù):')
dict[i]=score
print(dict)
3.11 輸入數(shù)字遏匆,打印數(shù)字的發(fā)音
dict={'1':'yi','2':'er','3':'san','4':'si','5':'wu','6':'liu','7':'qi','8':'ba','9':'jiu'}
content=input('請輸入數(shù)字:')
for i in content:
print(dict[i],end='--')
3.12 現(xiàn)在有一群主播: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)
別跑法挨,點個贊再走