Python之集合与字典

Python之集合与字典

  • 什么是集合

集合(set)是一个无序不重复元素的序列。
基本功能是进行成员关系测试和删除重复元素。
可以使用大括号 { } 或者 set() 函数创建集合,注意:创建一个空集合必须用 set() 而不是 { },因为 { } 是用来创建一个空字典。

  • 集合的定义

集合里面的元素不可重复的:

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

定义一个空集合:

>>> s2 = {}
>>> print(s2,type(s2))
({}, <type 'dict'>)
>>> s3 = set([])
>>> print(s3,type(s3))
(set([]), <type 'set'>)
>>> s4 = set()
>>> print(s4,type(s4))
(set([]), <type 'set'>)

列表去重:
因为集合的元素中是不可重复的,所以根据这一特性可以起到列表去重的目的

>>> li = [1,2,3,1,2,3,4,5,6,4,5,6]
>>> li1 = set(li)
>>> print(li1)
set([1, 2, 3, 4, 5, 6])
>>> print(list(set(li)))
[1, 2, 3, 4, 5, 6]
  • 集合的特性

集合的特性只支持成员操作符

>>> s = {1,2,3}
>>> print(1 in s)
True
>>> print(4 in s)
False
>>> print(1 not in s)
False

for循环遍历:

[kiosk@foundation1 ~]$ vim test.py 
[kiosk@foundation1 ~]$ cat test.py 
s = {1,2,3}
for i in s:
	print(i,end = '')
print()
[kiosk@foundation1 ~]$ /usr/local/python3/bin/python3 test.py 
123
  • 集合的常用方法

集合是一个可变的数据类型
添加顺序和在集合中存储的顺序不同

>>> s = {6,7,8,9,1,2,3}
>>> print(s)
set([1, 2, 3, 6, 7, 8, 9])

添加:

>>> s
set([1, 2, 3, 6, 7, 8, 9])
>>> s.add(10)
>>> s.add(0)
>>> print(s)
set([0, 1, 2, 3, 6, 7, 8, 9, 10])

增加多个元素:

>>> s
set([0, 1, 2, 3, 6, 7, 8, 9, 10])
>>> s.update({3,4,5,6,7,8})
>>> print(s)
set([0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10])

删除:

>>> s
set([0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10])
>>> s.pop()
0
>>> print(s)
set([1, 2, 3, 4, 5, 6, 7, 8, 9, 10])
>>> s.pop()
1
>>> print(s)
set([2, 3, 4, 5, 6, 7, 8, 9, 10])

删除指定元素 元素必须要存在:

>>> s
set([2, 3, 4, 5, 6, 7, 8, 9, 10])
>>> s.remove(11)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
KeyError: 11
>>> s.remove(10)
>>> print(s)
set([2, 3, 4, 5, 6, 7, 8, 9])

交集 并集 差集:

>>> s1 = {1,2,3}
>>> s2 = {2,3,4}

并集:

>>> print(s1.union(s2))
set([1, 2, 3, 4])
>>> print(s1|s2)
set([1, 2, 3, 4])

交集:

>>> print(s1.intersection(s2))
set([2, 3])
>>> print(s1&s2)
set([2, 3])

差集:
s1和s2的差集:s1中有哪些元素s2中没有
s2和s1的差集:s2中有哪些元素s1中没有

>>> print(s1.difference(s2))
set([1])
>>> print(s2.difference(s1))
set([4])
>>> print(s1-s2)
set([1])
>>> print(s2-s1)
set([4])

对等差分:并集 - 交集:

>>> print(s1.symmetric_difference(s2))
set([1, 4])
>>> print(s2.symmetric_difference(s1))
set([1, 4])
>>> print(s1^s2)
set([1, 4])
>>> print(s2^s1)
set([1, 4])

判断集合关系:

>>> s3 = {'redhat','westos'}
>>> s4 = {'redhat','westos','linux'}

s3是否s4的子集

>>> print(s3.issubset(s4))
True

s3是否s4的超集

>>> print(s3.issuperset(s4))
False

两个集合是不是不相交

>>> print(s3.isdisjoint(s4))
False
  • 集合的示例

华为机测题:
明明想在学校中请一些同学一起做一项问卷调查,为了实验的客观性
他先用计算机生成了N个1~1000之间的随机整数(N<=1000)
,N是用户输入的,对于
其中重复的数字,只保留一个,把其余相同的数字去掉,
不同的数对应着不同的学生的学号,
然后再把这些
数从小到大排序,按照排好的顺序去找同学做调查,
请你协助明明完成“去重”与排序工作

import random

s = set([])
for i in range(int(input('N:'))):
    s.add(random.randint(1,1000))
print(sorted(s))
  • python的常用方法

比较大小(内置):

>>> min(3,4)
3
>>> min(9,4)
4
>>> max(9,4)
9
>>> max(2,4)
4

累加求和:

>>> sum(range(1,101))
5050
>>> sum(range(1,101,2))
2500
>>> sum(range(2,101,2))
2550
  • 什么是字典

Python 内置了字典:dict 的支持,dict 全称 dictionary,使用键­值(key -value)存储,具有极快的查找速度。

  • 字典的定义

表示字典的几种方法:
枚举:返回索引值和对应的value值

[kiosk@foundation1 ~]$ vim test1.py 
[kiosk@foundation1 ~]$ cat test1.py 
for i,v in enumerate('hello'):
	print(str(i) + '----->' + v)
[kiosk@foundation1 ~]$ /usr/local/python3/bin/python3 test1.py 
0----->h
1----->e
2----->l
3----->l
4----->o

zip:

[kiosk@foundation1 ~]$ vim test2.py
[kiosk@foundation1 ~]$ cat test2.py
s1 = 'abc'
s2 = '123'
for i in zip(s1,s2):
    print(i)
for i in zip(s1,s2):
    print(''.join(i))
for i in zip(s1,s2):
    print('/'.join(i))
[kiosk@foundation1 ~]$ /usr/local/python3/bin/python3 test2.py 
('a', '1')
('b', '2')
('c', '3')
a1
b2
c3
a/1
b/2
c/3

字典:key-value 键值对

>>> s = {
... 'linux':[100,99,98],
... 'python':[190,56,78]
... }
>>> print(s,type(s))
({'python': [190, 56, 78], 'linux': [100, 99, 98]}, <type 'dict'>)

工厂函数:

>>> d = dict()
>>> print(d,type(d))
({}, <type 'dict'>)
>>> d = dict(a=1,b=2)
>>> print(d,type(d))
({'a': 1, 'b': 2}, <type 'dict'>)

字典的嵌套:

>>> stundet = {
... '123':{
... 'name':'tom',
... 'age':18,
... 'score':99
... },
... '456':{
... 'name':'lili',
... 'age':20,
... 'score':100
... }
... }
>>> print(stundet['123']['name'])
tom
>>> print(stundet)
{'123': {'age': 18, 'score': 99, 'name': 'tom'}, '456': {'age': 20, 'score': 100, 'name': 'lili'}}
  • 字典的特性

字典不支持切片:

>>> d = {'1':'a','2':'b'}
>>> print(d[1])
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
KeyError: 1
>>> print(d['1'])
a

成员操作符(针对key):

>>> d
{'1': 'a', '2': 'b'}
>>> print('1' in d)
True
>>> print('1' not in d)
False
>>> print('3' in d)
False
>>> print('a' in d)
False

for循环 针对key:

[kiosk@foundation1 ~]$ vim test3.py
[kiosk@foundation1 ~]$ cat test3.py 
d = {
    '1':'a',
    '2':'b'
}

for key in d:
    print(key)
[kiosk@foundation1 ~]$ /usr/local/python3/bin/python3 test3.py 
1
2

遍历字典

[kiosk@foundation1 ~]$ vim test4.py
[kiosk@foundation1 ~]$ cat test4.py 
d = {
    '1':'a',
    '2':'b'
}

for key in d:
    print(key,d[key])
[kiosk@foundation1 ~]$ /usr/local/python3/bin/python3 test4.py 
1 a
2 b
  • 字典元素的增加

增加一个元素:
如果key值存在,则更新对应的value值
如果key值不存在,则添加对应的key-value值

>>> service = {
... 'http':80,
... 'ftp':23,
... 'ssh':22
... }
>>> service['https'] = 443
>>> service
{'ftp': 23, 'http': 80, 'ssh': 22, 'https': 443}
>>> service['ftp'] = 21
>>> service
{'ftp': 21, 'http': 80, 'ssh': 22, 'https': 443}

增加多个key值:

>>> service_backup = {
... 'tomcat':8080,
... 'mysql':3306
... }
>>> service
{'ftp': 21, 'http': 80, 'ssh': 22, 'https': 443}
>>> service.update(service_backup)
>>> service
{'ftp': 21, 'http': 80, 'ssh': 22, 'https': 443, 'mysql': 3306, 'tomcat': 8080}
>>> service.update(flask=9000,dns=53)
>>> service
{'ftp': 21, 'dns': 53, 'http': 80, 'ssh': 22, 'https': 443, 'mysql': 3306, 'flask': 9000, 'tomcat': 8080}

setdefault添加:
如果key值存在:不做修改
如果key值不存在:则添加对应的key-value

>>> service
{'ftp': 21, 'dns': 53, 'http': 80, 'ssh': 22, 'https': 443, 'mysql': 3306, 'flask': 9000, 'tomcat': 8080}
>>> service.setdefault('http',9090)
80
>>> service
{'ftp': 21, 'dns': 53, 'http': 80, 'ssh': 22, 'https': 443, 'mysql': 3306, 'flask': 9000, 'tomcat': 8080}
>>> service.setdefault('oracle',4444)
4444
>>> service
{'ftp': 21, 'dns': 53, 'http': 80, 'ssh': 22, 'https': 443, 'mysql': 3306, 'flask': 9000, 'tomcat': 8080, 'oracle': 4444}
  • 字典元素的删除

del删除:指定key-value都删除

>>> service = {'http':80,'ftp':23,'ssh':22}
>>> del service['http']
>>> service
{'ftp': 23, 'ssh': 22}

pop删除:指定key对应的value值
如果key存在,删除,并且返回删除key对应的value(可以用变量取接收)
如果不存在,直接报错
popitem:删除最后一个key-value值

>>> service = {'http':80,'ftp':23,'ssh':22}
>>> item = service.pop('http')
>>> item
80
>>> service
{'ftp': 23, 'ssh': 22}
>>> item = service.pop('nfs')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
KeyError: 'nfs'
>>> service
{'ftp': 23, 'ssh': 22}
>>> item = service.popitem()
>>> item
('ftp', 23)
>>> service
{'ssh': 22}

清空字典内容:clear

>>> service = {'http':80,'ftp':23,'ssh':22}
>>> service.clear()
>>> service
{}
  • 字典元素的查看

查看字典的key值:

>>> service = {'http':80,'ftp':23,'ssh':22}
>>> service.keys()
['ftp', 'http', 'ssh']

查看字典的value值:

>>> service.values()
[23, 80, 22]

查看字典的key-value值:

>>> service.items()
[('ftp', 23), ('http', 80), ('ssh', 22)]

key不存在,默认返回None
key存在,default就返回defalut的值 没有就返回None

>>> service
{'ftp': 23, 'http': 80, 'ssh': 22}
>>> service.get('http')
80
>>> service.get('https')
>>> service.get('https','443')
'443'
  • 字典的示例

1.数字重复统计:
1). 随机生成1000个整数;
2). 数字的范围[20, 100],
3). 升序输出所有不同的数字及其每个数字重复的次数;

import random
all_num = []

for item in range(1000):
    all_num.append(random.randint(20,100))

# 对生成好的1000个数进行排序,然后加到子字典里
sorted_num = sorted(all_num)
num_dict ={}

for num in sorted_num:
    if num in num_dict:
        num_dict[num] += 1
    else:
        num_dict[num] = 1

print(num_dict)

2.重复的单词: 此处认为单词之间以空格为分隔符, 并且不包含,和.;
1). 用户输入一句英文句子;
2). 打印出每个单词及其重复的次数;

s = input('s:')
# 1.把每个单词分割处理
s_li = s.split()
print(s_li)
# 通过字典存储单词和改单词出现的次数
word_dict = {}
for item in s_li:
    if item not in word_dict:
        word_dict[item] = 1
    else:
        word_dict[item] += 1
print(word_dict)

3.随机生成100个卡号;
卡号以6102009开头, 后面3位依次是 (001, 002, 003, 100),
生成关于银行卡号的字典, 默认每个卡号的初始密码为"redhat";
输出卡号和密码信息, 格式如下:
卡号 密码
6102009001 000000

card_ids =[]
# 生成100个卡号
for i in range(100):
    # %.3d:代表整型数占3位
    s = '6102009%.3d' %(i+1)
    # 将每次生成的卡号都加入到列表中
    card_ids.append(s)

card_ids_dict = {}.fromkeys(card_ids,'redhat')
print(card_ids_dict)
print('卡号\t\t\t\t\t密码')
for key in card_ids_dict:
    print('%s\t\t\t%s' %(key,card_ids_dict[key]))

END

猜你喜欢

转载自blog.csdn.net/weixin_44828950/article/details/90678300