Ubuntu安装虚拟环境

Python不关心键-值对的添加顺序,而只关心键和值之间的关联关系。
pop()删除给定健对应的值,如:dict.pop(key) ,key不能为空

clear()清空字典内容,dict.clear()

popitem()随机删除字典内容, dict.popitem()

使用del语句删除字典的键-值对

代码:

fruits = {

    'a': 'apple',

    'b': 'banana',

   'c': 'coconut',

   'o': 'orange',

   'p': 'pear',

   'w': 'watermelon'

}

print(fruits)

current_fruit = fruits.pop('p')

print(current_fruit)

print(fruits)

current_fruit = fruits.pop('w')
print(current_fruit)

print(fruits)

del fruits['a']

print(fruits)

current_fruit = fruits.popitem()

print(current_fruit)

print(fruits)

fruits.clear()在这里插入代码片

print(fruits)

输出:

{'b': 'banana', 'w': 'watermelon', 'p': 'pear', 'o': 'orange', 'a': 'apple', 'c': 'coconut'}
pear
{'b': 'banana', 'w': 'watermelon', 'o': 'orange', 'a': 'apple', 'c': 'coconut'}
watermelon
{'b': 'banana', 'o': 'orange', 'a': 'apple', 'c': 'coconut'}
{'b': 'banana', 'o': 'orange', 'c': 'coconut'}
('b', 'banana')
{'o': 'orange', 'c': 'coconut'}
{}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值