Python – 字典、集合、元组

2024-08-13 0 947

这三个都是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 条件,我们可以访问特定的键并对该数据执行所需的操作

1

2

3

4

5

6

7

8

9

10

11

for product, product_count in inventory.items():

    print('\t\t6. 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'])

1

2

3

4

5

6

7

8

9

10

11

12

13

14

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 = 植物园中的“茉莉花”

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

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

1

2

3

4

5

6

7

8

9

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)

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

语法:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

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) 打赏

感谢您的支持,我会继续努力的!

打开微信/支付宝扫一扫,即可进行扫码打赏哦,分享从这里开始,精彩与您同在
点赞 (0)

免责声明
1. 本站所有资源来源于用户上传和网络等,如有侵权请邮件联系本站整改team@lcwl.fun!
2. 分享目的仅供大家学习和交流,您必须在下载后24小时内删除!
3. 不得使用于非法商业用途,不得违反国家法律。否则后果自负!
4. 本站提供的源码、模板、插件等等其他资源,都不包含技术服务请大家谅解!
5. 如有链接无法下载、失效或广告,请联系本站工作人员处理!
6. 本站资源售价或VIP只是赞助,收取费用仅维持本站的日常运营所需!
7. 如遇到加密压缩包,请使用WINRAR解压,如遇到无法解压的请联系管理员!
8. 因人力时间成本问题,部分源码未能详细测试(解密),不能分辨部分源码是病毒还是误报,所以没有进行任何修改,大家使用前请进行甄别!
9.本站所有源码资源都是经过本站工作人员人工亲测可搭建的,保证每个源码都可以正常搭建,但不保证源码内功能都完全可用,源码属于可复制的产品,无任何理由退款!

网站搭建学习网 Python Python – 字典、集合、元组 https://www.xuezuoweb.com/12538.html

常见问题
  • 本站所有的源码都是经过平台人工部署搭建测试过可用的
查看详情
  • 购买源码资源时购买了带主机的套餐是指可以享受源码和所选套餐型号的主机两个产品,在本站套餐里开通主机可享优惠,最高免费使用主机
查看详情

相关文章

发表评论
暂无评论
官方客服团队

为您解决烦忧 - 24小时在线 专业服务

Fa快捷助手
手机编程软件开发

在手机上用手点一点就能轻松做软件

去做软件
链未云主机
免备案香港云主机

开通主机就送域名的免备案香港云主机

去使用
链未云服务器
免备案香港云服务器

支持售后、超低价、稳定的免备案香港云服务器

去使用