python-认识(第四段-字典与集合)【下】

目录

3、字典的整体操作

(1)遍历

①遍历字典的键

②遍历字典中的值

③遍历字典所有条目

(2)字典的排序

(3)字典的合并

①使用for循环

②使用update()方法

③dict()函数

4、集合

①直接创建

②使用set()函数创建集合

③创建空集合

④集合的访问


3、字典的整体操作

(1)遍历

①遍历字典的键

        字典中用keys()的方法来返回字典中的所有的键。

演示:

>>> area={'els':1707,'jnd':997,'zg':960}
>>> for key in area.keys():
    print(key,area[key])
    
els 1707
jnd 997
zg 960

②遍历字典中的值

演示:

>>> for value in area.values():
    print(value)
    
1707
997
960

③遍历字典所有条目

        items()方法可以返回一个元组(键,值)。

演示:

>>> for item in area.items():
    print(item)
    
('els', 1707)
('jnd', 997)
('zg', 960)

(2)字典的排序

        严格来说,字典不支持排序。可以通过内置函数sorted()来显示结果,返回一个按照字母顺序排列的 列表 ,字典本身不会变化。

字典不能使用sorted()方法来修改排序,会报错。如:字典.sorted()

正确的方法: sorted(字典名)

演示:

>>> area={'Russia':1707,'Cananda':997,'China':960}
>>> ls = sorted(area)
>>> for country in ls:
    print(country,area[country])
    
Cananda 997
China 960
Russia 1707

(3)字典的合并

①使用for循环

 演示:

>>> area={'Russia':1707,'Cananda':997,'China':960}
>>> area_2={'American':936,'Baxi':854}
>>> for k,v in area_2.items():
    area [k] = v
    
>>> area
{'Russia': 1707, 'Cananda': 997, 'China': 960, 'American': 936, 'Baxi': 854}
>>> area_2
{'American': 936, 'Baxi': 854}

        for循环遍历了area_2字典中各个条目,并逐步添加到area字典中。

②使用update()方法

        update()方法用来将参数字典添加到调用方法的字典中。

格式:

字典名.update(参数字典名)

演示:

>>> area={'Russia':1707,'Cananda':997,'China':960}
>>> area_2={'American':936,'Baxi':854}
>>> area.update(area_2)
>>> area
{'Russia': 1707, 'Cananda': 997, 'China': 960, 'American': 936, 'Baxi': 854}

③dict()函数

        dict()函数将一组双元素序列转换为字典的方法。

        因此,可以先将两个字典通过items()转换为元组,再用list()函数变成列表,使二者相加连接,最后用dict()函数转换。

演示:

>>> area={'Russia':1707,'Cananda':997,'China':960}
>>> area_2={'American':936,'Baxi':854}
>>> ls=list(area.items())+list(area_2.items())
>>> area=dict(ls)
>>> area
{'Russia': 1707, 'Cananda': 997, 'China': 960, 'American': 936, 'Baxi': 854}
>>> area_2
{'American': 936, 'Baxi': 854}

        当然也有另一种方法。

>>> area={'Russia':1707,'Cananda':997,'China':960}
>>> area_2={'American':936,'Baxi':854}
>>> area=dict(area,**area_2)
>>> area
{'Russia': 1707, 'Cananda': 997, 'China': 960, 'American': 936, 'Baxi': 854}

4、集合

        集合与字典有些相似,直接将元素放在一对大括号里{};

        元素也是不可变的,因此不能使用列表作为元素。

①直接创建

演示:

>>> set1={1,2,3,4}
>>> set1
{1, 2, 3, 4}
>>> set2={(1,2),(3,4)}
>>> set2
{(1, 2), (3, 4)}

②使用set()函数创建集合

        set()用来将序列转换为集合,在转换的过程中重复的元素只能保留一个。

演示:

>>> s1=set('hello world')
>>> s1
{'h', 'e', 'd', ' ', 'w', 'l', 'o', 'r'}
>>> s2=set([1,2,3,2,1,4])
>>> s2
{1, 2, 3, 4}
>>> s3=set(1234)
Traceback (most recent call last):
  File "<pyshell>", line 1, in <module>
TypeError: 'int' object is not iterable

        s1是字符串去重后转换来的,s2是列表去重后转换来的,s3是整数转换为集合系统报错。

③创建空集合

>>> s1={}
>>> type(s1)
<class 'dict'>
>>> s2=set()
>>> type(s2)
<class 'set'>

        空集合不能用一对 "{}" 创建,而是要用不带参数的 set()

④集合的访问

        集合是无序的,所有集合的访问要么通过集合名整体输出,要么for循环遍历。

演示:

示例 生成20个0~20的随机数,并输出不同的数

import random
ls=[]
for i in range(20):
    ls.append(random.randint(0,20))
s=set(ls)
print("生成的随机数为:")
print(ls)
print(s)

生成的随机数为:
[5, 2, 14, 1, 9, 10, 17, 9, 4, 2, 18, 1, 18, 2, 2, 4, 12, 0, 3, 17]
{0, 1, 2, 3, 4, 5, 9, 10, 12, 14, 17, 18}

————————————————下一段: 函数

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值