1006- Python 字典(Dictionary)

本文详细介绍了Python字典的基础操作,包括如何创建、修改字典,以及如何对字典内的数据进行排序等实用技巧。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Python 字典的一些操作

目录

[TOC]来生成目录:

字典基础

字典是另一种可变容器模型,且可存储任意类型对象。

字典的每个键值(key=>value)对用冒号(:)分割,每个对之间用逗号(,)分割,整个字典包括在花括号({})中 ,格式如下所示:

d = {key1 : value1, key2 : value2 }
dict = {‘Alice’: ‘2341’, ‘Beth’: ‘9102’, ‘Cecil’: ‘3258’}

Markdown及扩展

代码块

inventory = {
    'gold' : 500,
    'pouch' : ['flint', 'twine', 'gemstone'], # Assigned a new list to 'pouch' key
    'backpack' : ['xylophone','dagger', 'bedroll','bread loaf']
}

# Adding a key 'burlap bag' and assigning a list to it
inventory['burlap bag'] = ['apple', 'small ruby', 'three-toed sloth']

# Sorting the list found under the key 'pouch'
inventory['pouch'].sort() 

# Your code here
inventory['pocket']=['seashell','strange berry','lint']
inventory['backpack'].sort() 
inventory['backpack'].remove('dagger') 
inventory['gold'] += 50
 Instructions

    01.Add a key to inventory called 'pocket'
    02.Set the value of 'pocket' to be a list consisting of the strings 'seashell', 'strange berry', and 'lint'
    03..sort() the items in the list stored under the 'backpack' key
    04.Then .remove('dagger') from the list of items stored under the 'backpack' key
    05.Add 50 to the number stored under the 'gold' key
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值