如何运用Python里的字典Dictionary

 时间:2026-02-11 23:10:11

1、新建一个JUPYTER NOTEBOOK文档。

如何运用Python里的字典Dictionary

2、可以先定义一个空集合的字典,然后再慢慢往里面添加关键词和数值。

d = {}

d["Peter"] = 88

d["Ben"] = 18

d["Alice"] = 20

print(d["Peter"])

print(d["Ben"])

print(d["Alice"])

如何运用Python里的字典Dictionary

3、没有定义好的是不能打印出来的,例如如下:

print(d["Apple"])

如何运用Python里的字典Dictionary

4、利用FOR IN 语句打印字典里面的关键词和值。

for key, value in d.items():

    print("key")

    print(key)

    print("value")

    print(value)

如何运用Python里的字典Dictionary

5、可以直接定义字典的内容。

phone_number = {"HE": "73647829874", "SHE": "47381049284", "THEY": "37294856371"}

print(phone_number)

如何运用Python里的字典Dictionary

6、可以改变关键词对应的数值。

phone_number["THEY"] = "WE"

print(phone_number)

如何运用Python里的字典Dictionary

7、通过用LEN可以知道关键词的数量。

len(d)

如何运用Python里的字典Dictionary

8、可以删除指定的关键词和数值。

del phone_number["HE"]

print(phone_number)

如何运用Python里的字典Dictionary

  • 如何利用python进行线性回归分析?
  • python怎么安装matplotlib
  • python怎么选取csv某几列
  • Anaconda下载及安装教程
  • 哈斯图怎么画?
  • 热门搜索
    萝卜干怎么做 金舵瓷砖怎么样 查尔斯顿眼镜怎么样 公司概况怎么写 怎么改善毛孔粗大 瓶盖打不开怎么办 冬瓜汤怎么做好吃 日语怎么打 我的世界光影怎么安装 喉咙痒干咳怎么办