Python学习笔记(5)

字典

在Python中,字典是一系列键值对,每个键都与一个值相关联,可以使用键来访问相关联的值,与键相关联的值可以是数、字符串、列表、字典等。

因此,字典可以将相关信息关联起来,同时可以存储的信息量几乎不受限制。

访问字典 

字典用放在花括号{}中的一系列键值对来表示。

访问字典时,若要获取与键相关联的值,应依次指定字典名和放在方括号[]内的键。

weather = {'today':'sunny','yesterday':'windy'}
print(weather['today'])
#输出
sunny

添加键值对

字典是一种动态结构,可以随时给字典添加键值对。

在添加键值对时,应依次指定字典名、放在方括号[] 内的键和与键相关联的值。同时,字典中各元素的排列顺序与定义时添加的键值对的顺序是相同的。

weather = {'today':'sunny','yesterday':'windy'}
print(weather)
weather['tomorrow'] = 'rainy'
print(weather)
#输出
{'today': 'sunny', 'yesterday': 'windy'}
{'today': 'sunny', 'yesterday': 'windy', 'tomorrow': 'rainy'}
weather = {}
print(weather)
weather['today'] = 'sunny'
weather['yesterday'] = 'windy'
print(weather)
#输出
{}
{'today': 'sunny', 'yesterday': 'windy'}

修改键值对

若要修改字典中某键相关联的值,应依次指定字典名、放在方括号[]内的键和与该键相关联的新值。 

weather = {'today':'sunny','yesterday':'windy'}
print(weather)
weather['today'] = 'rainy'
print(weather)
#输出
{'today': 'sunny', 'yesterday': 'windy'}
{'today': 'rainy', 'yesterday': 'windy'}

删除键值对

若要删除字典中的某个键值对,使用del语句,依次指定字典名和所要删除的键。

weather = {'today': 'sunny', 'yesterday': 'windy', 'tomorrow': 'rainy'}
print(weather)
del weather['tomorrow']
print(weather)
#输出
{'today': 'sunny', 'yesterday': 'windy', 'tomorrow': 'rainy'}
{'today': 'sunny', 'yesterday': 'windy'}

get()语句

若想访问字典中的键,但该键若不存在,系统就会报错。可以使用get()语句在指定的键不存在时返回一个默认值。

因此,get()语句中共有两个参数,第一个参数表示指定的键,第二个参数表示指定的键不存在时所要返回的值,若指定的键存在则会输出与该键相关联的值。

weather = {'today':'sunny','yesterday':'windy'}
weather_11 = weather.get('yesterday','Yes')
print(weather_11)
weather_22 = weather.get('tomorrow','NO')
print(weather_22)
#输出
windy
NO

遍历键值对

 若想遍历字典中所有的键值对,可以使用items()语句,利用for循环。

weather = {'today': 'sunny', 'yesterday': 'windy', 'tomorrow': 'rainy'}
for key,value in weather.items():
    print(f'\nKey:{key}')
    print(f'Value:{value}')
#输出
Key:today
Value:sunny

Key:yesterday
Value:windy

Key:tomorrow
Value:rainy

遍历字典中的键

若想遍历字典中所有的键,可以使用keys()语句,利用for循环。

weather = {'today': 'sunny', 'yesterday': 'windy', 'tomorrow': 'rainy'}
for key in weather.keys():
    print(f'\nKey:{key}')
#输出
Key:today

Key:yesterday

Key:tomorrow

遍历字典中的值

若想遍历字典中所有的值,可以使用values()语句,利用for循环。

weather = {'today': 'sunny', 'yesterday': 'windy', 'tomorrow': 'rainy'}
for value in weather.values():
    print(f'\nValue:{value}')
#输出
Value:sunny

Value:windy

Value:rainy

嵌套

 将字典存储在列表中,或者将列表作为值存储在字典中,这种方法称为嵌套。

weather_1 = {'today':'sunny','temperature':30}
weather_2 = {'yesterday':'windy','temperature':25}
weather_3 = {'tomorrow':'rainy','tempeerature':20}
weather_all = [weather_1 ,weather_2 ,weather_3]
for weather in weather_all:
               print(weather)
#输出
{'today': 'sunny', 'temperature': 30}
{'yesterday': 'windy', 'temperature': 25}
{'tomorrow': 'rainy', 'tempeerature': 20}

在字典中存储列表

pizza = {
    'crust':'thick',
    'toppings':['mushrooms','extra cheese'],
}
print(pizza['crust'])
for topping in pizza['toppings']:
    print(topping)
#输出
thick
mushrooms
extra cheese

在字典中存储字典

users = {
    'aeinstein':{
        'first':'albert',
        'last':'einstein',
        'location':'princeton',
    },
    'mcurie':{
         'first':'marie',
        'last':'curie',
        'location':'paris',
    },
}
for username,user_info in users.items():
    print(f'\nUsername:{username}')
    full_name = f"{user_info['first']} {user_info['last']}"
    location = user_info['location']
    print(f'\tFull name:{full_name.title()}')
    print(f'\tLocation:{location.title()}')
#输出
Username:aeinstein
	Full name:Albert Einstein
	Location:Princeton

Username:mcurie
	Full name:Marie Curie
	Location:Paris
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

~努力学习的小白~

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值