欢迎光临
我们一直在努力

Python - 字典、集合、元组

这三个都是python中不同类型的数据结构。这用于存储不同的数据集合。根据我们要求的用例,我们需要在其中进行选择。

Python - 字典、集合、元组

字典(dict):

  1. 字典是键值对的集合,其中每个键与一个值关联
  2. 可以根据键值检索数据(基于键的搜索),因为键要求是唯一的。
  3. 字典在 3.7 之前都是无序的,值可以更改。密钥名称不能直接更改

语法:
库存= {‘苹果’:20,’香蕉’:30,’胡萝卜’:15,’牛奶’:15}
print(‘t1.库存物品’, inventory)

可以使用以下语法添加另一个值/修改现有键的值

库存[‘鸡蛋’] = 20
库存[‘面包’] = 25
print(‘t2.更新的库存物品’, inventory)
库存[‘鸡蛋’]=库存[‘鸡蛋’]+5
print(‘t3.补货后’, 库存)

  • 可以使用 del 关键字从 dict 中删除数据。
  • 可以使用 in 关键字检查数据是否存在。结果将为布尔值。

删除库存[‘胡萝卜’]
del 库存[‘面包’]
print(‘t4.删除后更新库存’, inventory)

is_bananas_in_inventory = 库存中的“香蕉”
print(‘t5a.库存中有香蕉吗’, is_bananas_in_inventory)
is_oranges_in_inventory = 库存中有’橙色’
print(‘t5b.库存中是否有橙色’, is_oranges_in_inventory)

备注:
另外 dict.items() 会将字典中的每个项目作为元组(如键值对)给出。通过使用 list(dict.items()) 我们还可以获取列表形式的数据。使用 for 循环和 if 条件,我们可以访问特定的键并对该数据执行所需的操作

for product, product_count in inventory.items():
    print('tt6. product:', product, 'count is:', product_count)
print ('t7. iterating inventory gives tuple:', inventory.items())

#printing only egg count(value of key 'egg') by itearting dict
for product, product_count in inventory.items():
    if product is 'egg':
        print('t8. product:', product, ' its count is:', product_count)

#printing egg count (value of key 'egg')
print('t9. count of apple',inventory['egg'])
output:
        1. inventory items {'apple': 20, 'banana': 30, 'carrot': 15, 'milk': 15}
        2. updated inventory items {'apple': 20, 'banana': 30, 'carrot': 15, 'milk': 15, 'egg': 20, 'bread': 25}
        3. after restocking {'apple': 30, 'banana': 30, 'carrot': 15, 'milk': 15, 'egg': 25, 'bread': 25}
        4. updated inventory after delete {'apple': 30, 'banana': 30, 'milk': 15, 'egg': 25}
        5a. is banana in inventory true
        5b. is orange in inventory false
                6. product: apple count is: 30
                6. product: banana count is: 30
                6. product: milk count is: 15
                6. product: egg count is: 25
        7. iterating inventory gives tuple: dict_items([('apple', 30), ('banana', 30), ('milk', 15), ('egg', 25)])
        8. product: egg  its count is: 25
        9. count of apple 25

套装:
集合是唯一元素的无序集合。集合是可变的,但它们不允许重复的元素。

语法:
botanical_garden = {‘玫瑰’, ‘莲花’, ‘百合’}
botanical_garden.add(‘茉莉花’)
botanical_garden.remove(‘玫瑰’)
is_present_jasmine = 植物园中的“茉莉花”

上面我们可以看到定义了一个集合,添加了一个值并删除了它。如果我们在集合中添加相同的元素,则会出错。

我们还可以像维恩图一样比较两个集合。比如两个数据集的并集、差值、交集。

botanical_garden = {'tuple', 'rose', 'lily', 'jasmine', 'lotus'}
rose_garden = {'rose', 'lotus', 'hybiscus'}

common_flower= botanical_garden.intersection(rose_garden)
flowers_only_in_bg = botanical_garden.difference(rose_garden)
flowers_in_both_set = botanical_garden.union(rose_garden)

output will be a set by default. 
if needed we can typecase into list using list(expression)

元组:
元组是不可变的有序元素集合,这意味着它在创建后就无法更改。

语法:

ooty_trip = ('Ooty', '2024-1-1', 'Botanical_Garden')
munnar_trip = ('Munar', '2024-06-06', 'Eravikulam National Park')
germany_trip = ('Germany', '2025-1-1', 'Lueneburg')
print('t1. Trip details', ooty_trip, germany_trip)

#Accessing tuple using index
location = ooty_trip[0]
date = ooty_trip[1]
place = ooty_trip[2]

print(f't2a. Location: {location} Date: {date} Place: {place} ')

location, date, place =germany_trip # Assinging a tuple to 3 different variables
print(f't2b. Location: {location} Date: {date} Place: {place} ')

print('t3. The count of ooty_trip is ',ooty_trip.count)

Output:
   1. Trip details ('Ooty', '2024-1-1', 'Botanical_Garden') ('Germany', '2025-1-1', 'Lueneburg')
   2a. Location: Ooty Date: 2024-1-1 Place: Botanical_Garden
   2b. Location: Germany Date: 2025-1-1 Place: Lueneburg
   3. The count of ooty_trip is  <built-in method count of tuple object at></built-in>

可以使用索引访问元组。元组的值可以轻松地分配给多个变量。我们可以组合两个元组来创建另一个元组。但元组不能修改。

立即学习Python免费学习笔记(深入)”;

赞(0) 打赏
未经允许不得转载:码农资源网 » Python - 字典、集合、元组
分享到

觉得文章有用就打赏一下文章作者

非常感谢你的打赏,我们将继续提供更多优质内容,让我们一起创建更加美好的网络世界!

支付宝扫一扫打赏

微信扫一扫打赏

登录

找回密码

注册