Python之字典与集合的基本操作

一、字典的基本操作

1.1 空字典与字典更新

1.1.1 添加条目 

定义空字典即将一对空的大括号”{}“赋给字典变量。

 语法格式:字典名【键】 = 值

>>> dictArea = {}
>>> dictArea
{}
>>> dictArea['中国'] = 1200
>>> dictArea['美国'] = 1100
>>> dictArea['法国'] = 1000
>>> dictArea
{'中国': 1200, '美国': 1100, '法国': 1000}

1.1.2 修改条目 

语法格式:字典名【键】 = 值

>>> dictArea
{'中国': 1200, '美国': 1100, '法国': 1000}
>>> dictArea['法国'] = 900
>>> dictArea
{'中国': 1200, '美国': 1100, '法国': 900}

可以看出修改条目和添加条目是同一种操作,所以用于修改条目时,指定的键必须对应已存在的条目,否则就是添加条目。另外需要注意的是,修改条目实质上是修改与键关联的值,而具有唯一性的键是不可以被修改的。

1.2 删除字典条目 

删除字典条目的方法有很多,但是都是通过键来指定要删除的条目。 

 1.2.1 使用 del 命令删除指定条目

 语法格式:del 字典名【键】

>>> dictArea
{'中国': 1200, '美国': 1100, '法国': 900}
>>> del dictArea['法国']
>>> dictArea
{'中国': 1200, '美国': 1100}

1.2.2 使用 pop() 方法删除指定条目 

 语法格式:字典名.pop(键,默认值)

>>> dictArea
{'中国': 1200, '美国': 1100}
>>> dictArea.pop('美国')
1100
>>> dictArea
{'中国': 1200}

注意:使用pop()方法时,要保证删除的键在字典中存在,如果不确定是否存在时,需要给出默认值,否则会报错。另外,最少要包含一个用于指定键的参数,如果参数都缺省的话,系统会报错。 

 1.2.3 用popitem()方法随机删除字典条目

语法格式:字典名.popitem() 

>>> dictArea
{'中国': 1500, '美国': 1400, '法国': 1300, '日本': 1200, '英国': 1100}
>>> dictArea.popitem()
('英国', 1100)
>>> dictArea
{'中国': 1500, '美国': 1400, '法国': 1300, '日本': 1200}

删除的条目是以元组的形式返回。3.6版本之后,popitem()方法默认返回最后一个添加进字典的条目。 

1.2.4 用clear()方法清空字典条目 

 语法格式:字典名.clear()

>>> dictArea
{'中国': 1500, '美国': 1400, '法国': 1300, '日本': 1200}
>>> dictArea.clear()
>>> dictArea
{}

调用了clear()方法的字典虽然删除了所有条目,但依然是一个空字典。 

1.2.5 直接删除整个字典 

del 字典名 

>>> dictArea
{'中国': 1500, '美国': 1400, '法国': 1300, '日本': 1200, '英国': 1100}
>>> del dictArea
>>> dictArea
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
NameError: name 'dictArea' is not defined

1.3 查找字典条目 

 1.3.1 成员运算符 in

语法格式:键 in 字典 

>>> dictArea
{'中国': 1500, '美国': 1400, '法国': 1300, '日本': 1200, '英国': 1100}
>>> '中国' in dictArea
True
>>> '俄国' in dictArea
False

1.3.2 用 get() 方法获取条目的值 

字典名.get(键,默认值) 

>>> dictArea
{'中国': 1500, '美国': 1400, '法国': 1300, '日本': 1200, '英国': 1100}
>>> dictArea.get('中国')
1500
>>> dictArea.get('俄国')
>>> 

1.4 字典的遍历 

1.4.1 遍历字典中所有的键

字典有一个称为keys()的方法可以用来返回字典中的所有的键。

>>> dictAreas
{'中国': 1500, '美国': 1300, '英国': 1200, '法国': 1000}
>>> dictAreas.keys()
dict_keys(['中国', '美国', '英国', '法国'])
>>> for key in dictAreas.keys():
    print(key)

中国
美国
英国
法国
>>> for key in dictAreas.keys():
    print(key,dictAreas[key])

中国 1500
美国 1300
英国 1200
法国 1000

 1.4.2 遍历字典中所有的值

字典中也提供了一个用来返回所有值的方法values();

>>> dictAreas
{'中国': 1500, '美国': 1300, '英国': 1200, '法国': 1000}
>>> dictAreas.values()
dict_values([1500, 1300, 1200, 1000])
>>> for value in dictAreas.values():
    print(value)

1500
1300
1200
1000

  1.4.3 遍历字典中所有的条目 

字典的items()方法能以(键,值)的形式返回所有条目;

>>> dictAreas
{'中国': 1500, '美国': 1300, '英国': 1200, '法国': 1000}
>>> dictAreas.items()
dict_items([('中国', 1500), ('美国', 1300), ('英国', 1200), ('法国', 1000)])
>>> for item in dictAreas.items():
    print(item)

('中国', 1500)
('美国', 1300)
('英国', 1200)
('法国', 1000)

1.5 字典的排序 

字典里的条目是没有顺序的,严格来说,字典是不支持排序的,我们只能将字典中的条目按照我们希望的顺序进行显示。

由于中文的排序涉及到编码的问题,所以将键改为英文。

>>> dictArea
{'China': 1500, 'Russia': 1300, 'Canada': 1200}
>>> sorted(dictArea)
['Canada', 'China', 'Russia']
>>> dictArea
{'China': 1500, 'Russia': 1300, 'Canada': 1200}
>>> ls = sorted(dictArea)
>>> ls
['Canada', 'China', 'Russia']
>>> for country in ls:
    print(country,dictArea[country])

Canada 1200
China 1500
Russia 1300

1.6 字典的合并 

1.6.1 使用for循环

>>> dictArea1
{'中国': 1500, '法国': 1300, '美国': 1200, '英国': 1000}
>>> dictArea2
{'张三': 1100, '李四': 1000, '王五': 900, '小阿丁': 500}
>>> for k,v in dictArea2.items():
    dictArea1[k] = v

>>> dictArea1
{'中国': 1500, '法国': 1300, '美国': 1200, '英国': 1000, '张三': 1100, '李四': 1000, '王五': 900, '小阿丁': 500}
>>> dictArea2
{'张三': 1100, '李四': 1000, '王五': 900, '小阿丁': 500}

 1.6.2 使字典的update()方法

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

语法格式:字典名.update(参数字典名) 

>>> dictArea1
{'中国': 1500, '法国': 1300, '美国': 1200, '英国': 1000}
>>> dictArea2
{'张三': 1100, '李四': 1000, '王五': 900, '小阿丁': 500}
>>> dictArea1.update(dictArea2)
>>> dictArea1
{'中国': 1500, '法国': 1300, '美国': 1200, '英国': 1000, '张三': 1100, '李四': 1000, '王五': 900, '小阿丁': 500}
>>> dictArea2
{'张三': 1100, '李四': 1000, '王五': 900, '小阿丁': 500}

  1.6.3 使用dict()函数 

>>> dictArea1
{'中国': 1500, '法国': 1300, '美国': 1200, '英国': 1000}
>>> dictArea2
{'张三': 1100, '李四': 1000, '王五': 900, '小阿丁': 500}
>>> ls = list(dictArea1.items()) + list(dictArea2.items())
>>> ls
[('中国', 1500), ('法国', 1300), ('美国', 1200), ('英国', 1000), ('张三', 1100), ('李四', 1000), ('王五', 900), ('小阿丁', 500)]
>>> dictArea = dict(ls)
>>> dictArea
{'中国': 1500, '法国': 1300, '美国': 1200, '英国': 1000, '张三': 1100, '李四': 1000, '王五': 900, '小阿丁': 500}
>>> dictArea1
{'中国': 1500, '法国': 1300, '美国': 1200, '英国': 1000}
>>> dictArea2
{'张三': 1100, '李四': 1000, '王五': 900, '小阿丁': 500}

  1.6.4 使用dict()函数的另一种形式  

>>> dictArea1
{'中国': 1500, '法国': 1300, '美国': 1200, '英国': 1000}
>>> dictArea2
{'张三': 1100, '李四': 1000, '王五': 900, '小阿丁': 500}
>>> dictArea1 = dict(dictArea1,**dictArea2)
>>> dictArea1
{'中国': 1500, '法国': 1300, '美国': 1200, '英国': 1000, '张三': 1100, '李四': 1000, '王五': 900, '小阿丁': 500}
>>> dictArea2
{'张三': 1100, '李四': 1000, '王五': 900, '小阿丁': 500}

注意:一般来说,参加合并的两个字典中的键都是不同的,如果两个字典中出现了相同的键,那么合并后只会有一组包含改键的条目被保留下来。

二、集合的基本操作 

 2.1 添加元素

 函数或方法:s,add(item) 或者s.update(item)

区别:s.add(item)将参数item作为元素添加到集合s中,如果item是序列,则将其作为一个元素整体加入集合。作为参数的item只能是不可变的数据。

s.update(item)将参数序列item中的元素拆分去重后加入集合。参数item可以是可变数据。

>>> s = {1,2,3}
>>> s.add((2,3,4))
>>> s
{1, 2, 3, (2, 3, 4)}
>>> s.add([2,3,4])
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: unhashable type: 'list'
>>> s = {1,2,3}
>>> s.update([2,3,4])
>>> s
{1, 2, 3, 4}

 2.2 删除元素 

 函数或方法:s.remove(item) 或 s.discard(item) 或 s.pop() 或 s.clear()

>>> s = {1,2,3,4}
>>> s.remove(2)
>>> s
{1, 3, 4}
>>> s.remove(5)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
KeyError: 5
>>> s = {1,2,3,4}
>>> s.discard(3)
>>> s
{1, 2, 4}
>>> s.discard(5)
>>> 
>>> s = {1,2,3,4}
>>> s.pop()           //随机删除
1
>>> s = {1,2,3,4}
>>> s.clear()
>>> s
set()

  2.3 成员判断 

语法格式:item in s 

 2.4 集合的数学运算  

2.4.1 求并集 

 运算符:A | B  

方法:A。union(B)

>>> A = {1,2,3,4}
>>> B = {3,4,5,6}
>>> A|B
{1, 2, 3, 4, 5, 6}
>>> A
{1, 2, 3, 4}
>>> B
{3, 4, 5, 6}
>>> A.union(B)
{1, 2, 3, 4, 5, 6}
>>> B
{3, 4, 5, 6}

 2.4.2 求交集 

运算符:A & B

方法:A。intersection(B) 

>>> A
{1, 2, 3, 4}
>>> B
{3, 4, 5, 6}
>>> A & B
{3, 4}
>>> A
{1, 2, 3, 4}
>>> B
{3, 4, 5, 6}
>>> A.intersection(B)
{3, 4}

  2.4.3 求差集 

运算符:A - B

方法:A.difference(B)  

>>> A
{1, 2, 3, 4}
>>> B
{3, 4, 5, 6}
>>> A - B
{1, 2}
>>> A
{1, 2, 3, 4}
>>> B
{3, 4, 5, 6}

   2.4.4 求对称差集 

 运算符:A ^ B

方法:A.symmetric_difference(B)

>>> A
{1, 2, 3, 4}
>>> B
{3, 4, 5, 6}
>>> A.symmetric_difference(B)
{1, 2, 5, 6}
>>> A
{1, 2, 3, 4}
>>> B
{3, 4, 5, 6}

 

猜你喜欢

转载自blog.csdn.net/m0_51769031/article/details/127476677