日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > python >内容正文

python

Python四大金刚之二:字典

發(fā)布時間:2023/12/10 python 36 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Python四大金刚之二:字典 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

引言

?列表、字典:可變序列,可以執(zhí)行增刪改排序等

字典:無序的

?

?一、字典的創(chuàng)建

#使用{}創(chuàng)建 scores = {'張三':100 ,'李四':98 ,'王麻子':72} print(scores) print(type(scores))#使用內(nèi)置函數(shù)dict() student = dict(name = 'jack ', age = 16) print(student) print(type(student))

二、字典元素的操作

(一)獲取

#獲取字典中的元素 #方法一: print(scores['張三'])#方法二: print(scores.get('張三')) print(scores.get('66')) #如果查找的不存在,返回none

(二)增刪改

?刪除操作

del scores['張三'] #根據(jù)索引刪除 key 和value print(scores)scores.clear() #刪除所有 print(scores)

新增操作 (直接增加)

scores['趙四'] = 80

三、獲取字典的視圖

# 獲取所有key值 key = scores.keys() print(key) print(type(key)) print(list(key)) #將key組成的視圖轉(zhuǎn)成list#獲取所有value值 value = scores.values() print(value) print(type(value)) print(list(value)) #將value組成的視圖轉(zhuǎn)成list#獲取所有的key-value值 items = scores.items() print(items) print(type(items)) print(list(items)) #轉(zhuǎn)換為list后元素由元組組成

四、字典的遍歷

? ?

for item in scores :print(item,end=' ') #輸出的是字典中的key#輸出key對應(yīng)的valueprint(scores[item],end=' ')print(scores.get(item))

? 五、字典的特點

?六、字典生成式

students = ['mark','sheep','jerry','tom'] grades = [100,78,60,59]d={key:price for key,price in zip(students,grades)} print(d)

總結(jié)

以上是生活随笔為你收集整理的Python四大金刚之二:字典的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。