首页 > 编程 > Python > 正文

Python -- 5. 字典

2019-11-08 01:11:51
字体:
来源:转载
供稿:网友

1. 使用字典 字典 是一系列键—值对 。每个键 都与一个值相关联,你可以使用键来访问与之相关联的值。 字典用放在花括号{} 中的一系列键—值对表示,键和值之间用冒号分隔,而键—值对之间用逗号分隔。

alien_0 = {'color': 'green', 'points': 5}

(1).访问字典中的值 要获取与键相关联的值,可依次指定字典名和放在方括号内的键

alien_0 = {'color': 'green', 'points': 5}new_points = alien_0['points']PRint("You just earned " + str(new_points) + " points!")

(2).添加键—值对 字典是一种动态结构,可随时在其中添加键—值对。 要添加键—值对,可依次指定字典名、用方括号括起的键和相关联的值。

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

(3).修改字典中的值 要修改字典中的值,可依次指定字典名、用方括号括起的键以及与该键相关联的新值.

alien_0 = {'color': 'green'}print("The alien is " + alien_0['color'] + ".")alien_0['color'] = 'yellow'print("The alien is now " + alien_0['color'] + ".")

(4).删除键—值对 可使用del 语句将相应的键—值对彻底删除。

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

2. 遍历字典 (1).遍历所有的键—值对 方法items(), 它返回一个键—值对列表。

user_0 = {'username': 'efermi','first': 'enrico','last': 'fermi',}for key, value in user_0.items(): print("/nKey: " + key) print("Value: " + value)

(2).遍历字典中的所有键 在不需要使用字典中的值时,方法keys() 很有用

favorite_languages = {'jen': 'python','sarah': 'c','edward': 'ruby','phil': 'python',}for name in favorite_languages.keys(): print(name.title())

(3).按顺序遍历字典中的所有键 字典总是明确地记录键和值之间的关联关系,但获取字典的元素时,获取顺序是不可预测的。 为此,可使用函数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.")

(4).遍历字典中的所有值 可使用方法values() ,它返回一个值列表,而不包含任何键。

favorite_languages = {'jen': 'python','sarah': 'c','edward': 'ruby','phil': 'python',}print("The following languages have been mentioned:")for language in favorite_languages.values(): print(language.title())

最终的列表可能包含大量的重复项。为剔除重复项,可使用集合(set)。 集合 类似于列表,但每个元素都必须是独一无二的.

favorite_languages = {'jen': 'python','sarah': 'c','edward': 'ruby','phil': 'python',}print("The following languages have been mentioned:")for language in set(favorite_languages.values()): print(language.title())

3. 嵌套 将一系列字典存储在列表中,或将列表作为值存储在字典中,这称为嵌套 (1). 字典列表

alien_0 = {'color': 'green', 'points': 5}alien_1 = {'color': 'yellow', 'points': 10}alien_2 = {'color': 'red', 'points': 15}aliens = [alien_0, alien_1, alien_2]for alien in aliens: print(alien)

(2).在字典中存储列表

# 存储所点比萨的信息pizza = {'crust': 'thick','toppings': ['mushrooms', 'extra cheese'],}# 概述所点的比萨print("You ordered a " + pizza['crust'] + "-crust pizza " +"with the following toppings:")for topping in pizza['toppings']: print("/t" + topping)

(3).在字典中存储字典

users = { 'aeinstein': { 'first': 'albert', 'last': 'einstein', 'location': 'princeton', }, 'mcurie': { 'first': 'marie', 'last': 'curie', 'location': 'paris', },}for username, user_info in users.items(): print("/nUsername: " + username) full_name = user_info['first'] + " " + user_info['last'] location = user_info['location'] print("/tFull name: " + full_name.title()) print("/tLocation: " + location.title())
上一篇:python记事

下一篇:八大排序算法_python

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表