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

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程语言 > python >内容正文

python

Python数据类型--字典

發(fā)布時(shí)間:2024/7/23 python 26 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Python数据类型--字典 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

Python中的字典

  • 存儲(chǔ)數(shù)據(jù)時(shí)不保持元素的順序
  • 通過任意鍵值查找集合中值信息的過程叫做映射,Python中通過字典實(shí)現(xiàn)映射。
  • 字典是一個(gè)鍵值對(duì)的集合。
    – 該集合以鍵為索引,同一個(gè)鍵信息對(duì)應(yīng)一個(gè)值。
>>> passwd = {"China": "BigCountry", "Korean": "SmallCountry"} >>> print(passwd) {'China': 'BigCountry', 'Korean': 'SmallCountry'}

字典操作

為字典增加一項(xiàng)

dictionaryName[key] = value
使用大括號(hào)創(chuàng)建字典,使用中括號(hào)為字典增加一項(xiàng)

>>> students = {" 203":"John", "204":"Peter"} >>> students["205"] = "Susan" >>> print(students) {' 203': 'John', '204': 'Peter', '205': 'Susan'}
訪問字典中的值

dictionaryName[key] 返回鍵key對(duì)應(yīng)的值value,如果沒有該項(xiàng),則出錯(cuò)。

>>> students["204"] 'Peter' >>> students["206"] Traceback (most recent call last):File "<input>", line 1, in <module> KeyError: '206'
刪除字典中的一項(xiàng)

del dictionaryName[key]

>>> del students[" 203"]
字典的遍歷
>>> for key in students: ... print(key + " :" + str(students[key])) ... 204 :Peter 205 :Susan
  • 遍歷字典的鍵key
for key in dictionaryName.keys():print(key)
  • 遍歷字典的值value
for value in dictionaryName.values():print(value)
  • 遍歷字典的項(xiàng)
for item in dictionaryName.items():print(item)
  • 遍歷字典的key-value對(duì)
for item, value in dictionaryName.items():print(item, value)
  • 判斷一個(gè)鍵是否在字典中
in 或者not in
  • 字典的標(biāo)準(zhǔn)操作符
-, <, >, <=, >=, ==, !=, and, or, not
  • 字典不支持拼接操作符和重復(fù)操作符

總結(jié)

以上是生活随笔為你收集整理的Python数据类型--字典的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。

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