Python3 字典

逆流者 2020年11月02日 120次浏览

一个简单的字典

alien_0 = {'color': 'green', 'points': 5}
print(alien_0)
print(alien_0['color'])
print(alien_0['points'])
{'color': 'green', 'points': 5}
green
5

使用字典

上面的列子中:alien_0 = {'color': 'green', 'points': 5}
使用花括号{}表示字典, 里面是一系列键-值对

访问字典中的值

alien_0['color']

添加键-值对

alien_0 = {'color': 'green', 'points': 5}
print(alien_0)
alien_0['x_position'] = 0
alien_0['y_position'] = 25
print(alien_0)
{'color': 'green', 'points': 5}
{'color': 'green', 'points': 5, 'x_position': 0, 'y_position': 25}

创建一个空字典

alien_0 = {}
alien_0['color'] = 'grenn'
alien_0['points'] = 5
print(alien_0)
{'color': 'grenn', 'points': 5}

修改字典中的值

alien_0 = {'color': 'green', 'points': 5}
print(alien_0)
alien_0['color'] = 'yellow'
print(alien_0)
{'color': 'green', 'points': 5}
{'color': 'yellow', 'points': 5}

删除键-值对

alien_0 = {'color': 'green', 'points': 5}
print(alien_0)
del alien_0['color']
print(alien_0)
{'color': 'green', 'points': 5}
{'points': 5}

删除的键-值对永远消失了

遍历字典

遍历所有的键-值对

user_0 = {
    'username': 'wsh',
    'first': 'wh',
    'last': 'shanghui'
}

for key, value in user_0.items():
    print('\nkey: ' + key)
    print('value: ' + value)

key, value 不是固定的, 你也可以定义成k, v或者其他变量名

key: username
value: wsh

key: first
value: wh

key: last
value: shanghui

再看一个例子:

favorite_languages = {'jen': 'python',
                      'sarah': 'c',
                      'edward': 'ruby',
                      'phil': 'python'
                      }

for name, languages in favorite_languages.items():
    print(name.title() + "'s favorite languages is " + languages.title() + '.')
Jen's favorite languages is Python.
Sarah's favorite languages is C.
Edward's favorite languages is Ruby.
Phil's favorite languages is Python.

遍历字典中的所有键

favorite_languages = {'jen': 'python',
                      'sarah': 'c',
                      'edward': 'ruby',
                      'phil': 'python'
                      }

for name in favorite_languages.keys():
    print(name.title())
Jen
Sarah
Edward
Phil

按顺序遍历字典中的所有键

使用sorted()函数对键进行排序

favorite_languages = {'jen': 'python',
                      'sarah': 'c',
                      'edward': 'ruby',
                      'phil': 'python'
                      }

for name in sorted(favorite_languages.keys()):
    print(name.title() + ', thank you for taking the poll.')
Edward, thank you for taking the poll.
Jen, thank you for taking the poll.
Phil, thank you for taking the poll.
Sarah, thank you for taking the poll.

遍历字典中的所有值

favorite_languages = {'jen': 'python',
                      'sarah': 'c',
                      'edward': 'ruby',
                      'phil': 'python'
                      }

for languages in favorite_languages.values():
    print(languages.title())
Python
C
Ruby
Python

嵌套

字典列表

alien_0 = {'color': 'green', 'points': 5}
alien_1 = {'color': 'red', 'points': 10}
alien_2 = {'color': 'yellow', 'points': 15}

aliens = [alien_0, alien_1, alien_2]

for alien in aliens:
    print(alien)

{'color': 'green', 'points': 5}
{'color': 'red', 'points': 10}
{'color': 'yellow', 'points': 15}

在字典中存储列表

favorite_languages = {'jen': ['python', 'java'],
                      'sarah': 'c',
                      'edward': ['ruby', 'go'],
                      'phil': ['python', 'c++']
                      }

for name, languages in favorite_languages.items():
    print('\n' + name.title() + "'s favorite languages are: ")
    for language in languages:
        print('\t' + language.title())
Jen's favorite languages are: 
	Python
	Java

Sarah's favorite languages are: 
	C

Edward's favorite languages are: 
	Ruby
	Go

Phil's favorite languages are: 
	Python
	C++

在字典中存储字典

users = {
    'user1': {
        'first': 'xiao',
        'last': 'ming',
        'location': 'shanghai'
    },
    'user2': {
        'first': 'hong',
        'last': 'hong',
        'location': 'beijing'
    },
}

for username, user_info in users.items():
    print('\nUsername: ' + username)
    full_name = user_info['first'] + ' ' + user_info['last']
    location = user_info['location']

    print('\t Full name: ' + full_name.title())
    print('\tLocation: ' + location.title())

Username: user1
	 Full name: Xiao Ming
	Location: Shanghai

Username: user2
	 Full name: Hong Hong
	Location: Beijing

Python3 目录

  1. Python3 教程
  2. Python3 变量和简单数据类型
  3. Python3 列表
  4. Python3 操作列表
  5. Python3 if 语句
  6. Python3 if 字典
  7. Python3 用户输入和while循环
  8. Python3 函数
  9. Python3 类
  10. Python3 文件和异常
  11. Python3 测试代码
  12. Python3 使用matplotlib绘制图表
  13. Python3 使用Pygal生成矢量图形文件
  14. Python3 使用csv模块处理CSV(逗号分割的值)格式存储的天气数据
  15. Python3 处理JSON格式数据(制作交易收盘价走势图)
  16. Python3 使用API