Python学习笔记-字典

1、说明
  • 字典是描述 Key-Value 的容器。
  • 每个键都有对应的值,是一对一的关系。
  • 可以使用键来访问值,这里的值可以使数字、字符串、列表乃至字典。
  • 键值通过在花括号"{ … }“中定义,键和值之间用冒号”:"号分隔,键值对之间用逗号“,”分隔。
2、基础使用
  1. 简单的字典
alien_0 = {'color':'green', 'points':5 }
print(alien_0['color'])
print(alien_0['points'])
print(alien_0)
  1. 创建空字典
alien_0 = {}
  1. 添加键值对
alien_0['x_position'] = 0
alien_0['y_position'] = 25
print(alien_0)
  1. 修改字典的值
alien_0 = {'color':'green'}
alien_0['color'] = 'yellow'
print(alien_0)
  1. 删除键值对
alien_0 = {'color':'green', 'points':5 }
print(alien_0)

del alien_0['points']
print(alien_0)
  1. 使用字典来存储众多对象的同一种信息
favorite_languages = {
	'jen' : 'python',
	'sarah' : 'c',
	'edward' : 'ruby',
	'phil' : 'python'
}
  1. 遍历字典
  • 遍历所有键值对
user_0 = {
	'username' : 'feven',
	'first' : 'hw',
	'last' : 'h'
}
for key, value in user_0.items():
	print("\nKey: " + key)
	print("Value: " + value)
  • 遍历所有键
favorite_languages = {
	'jen' : 'python',
	'sarah' : 'c',
	'edward' : 'ruby',
	'phil' : 'python'
}
for name in favorite_languages.keys():		#keys()返回一个列表,其中包含字典中所有键
	print(name.title())
for name in favorite_languages:					
	print(name.title())
  • 按顺序遍历字典中的所有键
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.")
  • 遍历字典中的所有值
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())
print('-'*20)
for language in set(favorite_languages.values()):	#使用set()剔除重复项
	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)
  1. 在字典中存储列表
#使用字典存储顾客点的披萨信息
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)

#使用字典存储喜欢的编程语言
favorite_languages = {
	'jen' : ['python', 'ruby'],
	'sarah' : ['c'],
	'edward' : ['ruby', 'go'],
	'phil' : ['python', 'haskell']
}
for name, languages in favorite_languages.items():
	print("\n" + name.title() + "'s favorite languages are:")
	for language in languages:
		print("\t" + language.title())
  1. 在字典中存储字典
users = {
	'aeinstein': {
		'first': 'albert',
		'last': 'einstein',
		'location': 'princeton'
	},
	'mcurie': {
		'first': 'marie',
		'last': 'curie',
		'location': 'paris'
	}
}
for username, user_info in users.items():
	print("\n Username: " + username)
	full_name = user_info['first'] + " " + user_info['last']
	location = user_info['location']
	
	print("\t full name: " + full_name.title())
	print("\t location: " + location.title())
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值