第五章 字典和集合
第五章 字典和集合
一、字典
1、创建字典:
>>> aDict={'ip':'10.5.17.12','port':'21'}
>>> adict
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'adict' is not defined. Did you mean: 'aDict'?
>>> aDict
{'ip': '10.5.17.12', 'port': '21'}
>>> x=dict()
>>> x={}
>>> keys={'a','b','c','d'}
>>> values=[1,2,3,4]
>>> xDict=dict(zip(keys,values))
>>> xDict
{'c': 1, 'b': 2, 'd': 3, 'a': 4}
>>> aDict=dict(name='dog',age='3')
>>> aDict=dict.fromKeys(['name','age','set'])
>>> aDict=dict('name'='dog','age'='3')
>>> aDict
{'name': 'dog', 'age': '3'}
>>> aDict=dict.fromKeys(['name','age','sex'])
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: type object 'dict' has no attribute 'fromKeys'. Did you mean: 'fromkeys'?
>>> aDict=dict.fromkeys(['name','age','sex'])
>>> aDict
{'name': None, 'age': None, 'sex': None}
>>> del aDict
>>> words=['Hello','China','CaoCao']
#推导式生产字典
>>> a={word:word.upper()for word in words }
>>> a
{'Hello': 'HELLO', 'China': 'CHINA', 'CaoCao': 'CAOCAO'}
>>>
2、访问字典
>>> aDict=dict('name'='dog','age'='3')
>>> aDict('age')
>>> aDict.get('age','Not Exists')
>>> aDict.items()
3、字典元素增删改
#修改
>>> sock={'ip':'10.5.17.12','port':'21'}
>>> sock['port']=8000
#添加
>>> sock['protocol']='UDP'
#删除
>>> sock.pop('protocol')
>>> del sock['port']
#随机删除
>>> sock.popitem()
4、字典应用
from random import randrange
#存在大量用户对大量电影评分,
#randrange,随机生成一个数
#给10个人,生成3~10部电影的评分数据,评分(1~5分)
data={'user'+str(i):{'film'+str(randrange(1,15)):
randrange(1,6)
for j in range(randrange(3,10))}
for i in range(10)}
#生成5条,电影的是用户评分(1-5分)
user={'film'+str(randrange(1,15)):randrange(1,6) for i in range(5)}
#lambda 匿名函数
#范围 输入Item
#最相似的用户及其对电影的打分情况
#要求两个用户共同打分的电影最多,并且所有电影打分差值的平方和最小
f=lambda item:(-len(item[1].keys()&user),
sum(((item[1].get(film)-user.get(film))**2 for film in user.keys()&item[1].keys())))
#key是规则
#
similarUser,films=min(data.items(),key=f)
#
print('Known data'.center(50,'='))
for item in data.items():
print(len(item[1].keys()&user.keys()),
sum(((item[1].get(film)-user.get(film))**2 for film in user.keys()&item[1].keys())),
item,
sep=':')
print('current user'.center(50,'='))
print(user)
print('most similar user and his film'.center(50,'='))
print(similarUser,films,sep=':')
print('recommended film'.center(50, '='))
print(max(films.keys()-user.keys(),key=lambda film:films[film]))
====================Known data====================
2:0:('user0', {'film9': 3, 'film6': 5, 'film14': 1, 'film10': 5})
===================current user===================
{'film2': 3, 'film8': 4, 'film9': 3, 'film10': 5}
==========most similar user and his film==========
user4:{'film2': 5, 'film4': 2, 'film3': 3, 'film12': 3, 'film9': 1, 'film8': 2}
=================recommended film=================
film12
2:4:('user1', {'film5': 5, 'film11': 2, 'film7': 4, 'film9': 5, 'film8': 4})
===================current user===================
{'film2': 3, 'film8': 4, 'film9': 3, 'film10': 5}
==========most similar user and his film==========
user4:{'film2': 5, 'film4': 2, 'film3': 3, 'film12': 3, 'film9': 1, 'film8': 2}
=================recommended film=================
film12
1:9:('user2', {'film8': 1, 'film1': 4, 'film5': 2, 'film13': 5, 'film4': 1, 'film11': 5, 'film14': 2})
===================current user===================
{'film2': 3, 'film8': 4, 'film9': 3, 'film10': 5}
==========most similar user and his film==========
user4:{'film2': 5, 'film4': 2, 'film3': 3, 'film12': 3, 'film9': 1, 'film8': 2}
=================recommended film=================
film12
1:4:('user3', {'film8': 2, 'film13': 2, 'film12': 4, 'film1': 5, 'film7': 3})
===================current user===================
{'film2': 3, 'film8': 4, 'film9': 3, 'film10': 5}
==========most similar user and his film==========
user4:{'film2': 5, 'film4': 2, 'film3': 3, 'film12': 3, 'film9': 1, 'film8': 2}
=================recommended film=================
film12
3:12:('user4', {'film2': 5, 'film4': 2, 'film3': 3, 'film12': 3, 'film9': 1, 'film8': 2})
===================current user===================
{'film2': 3, 'film8': 4, 'film9': 3, 'film10': 5}
==========most similar user and his film==========
user4:{'film2': 5, 'film4': 2, 'film3': 3, 'film12': 3, 'film9': 1, 'film8': 2}
=================recommended film=================
film12
1:9:('user5', {'film8': 1, 'film1': 1, 'film4': 1, 'film12': 5})
===================current user===================
{'film2': 3, 'film8': 4, 'film9': 3, 'film10': 5}
==========most similar user and his film==========
user4:{'film2': 5, 'film4': 2, 'film3': 3, 'film12': 3, 'film9': 1, 'film8': 2}
=================recommended film=================
film12
1:4:('user6', {'film10': 3, 'film7': 4, 'film5': 2, 'film12': 3})
===================current user===================
{'film2': 3, 'film8': 4, 'film9': 3, 'film10': 5}
==========most similar user and his film==========
user4:{'film2': 5, 'film4': 2, 'film3': 3, 'film12': 3, 'film9': 1, 'film8': 2}
=================recommended film=================
film12
1:0:('user7', {'film7': 5, 'film3': 5, 'film9': 3, 'film5': 3, 'film4': 1, 'film6': 2})
===================current user===================
{'film2': 3, 'film8': 4, 'film9': 3, 'film10': 5}
==========most similar user and his film==========
user4:{'film2': 5, 'film4': 2, 'film3': 3, 'film12': 3, 'film9': 1, 'film8': 2}
=================recommended film=================
film12
1:1:('user8', {'film5': 3, 'film12': 1, 'film10': 4})
===================current user===================
{'film2': 3, 'film8': 4, 'film9': 3, 'film10': 5}
==========most similar user and his film==========
user4:{'film2': 5, 'film4': 2, 'film3': 3, 'film12': 3, 'film9': 1, 'film8': 2}
=================recommended film=================
film12
0:0:('user9', {'film1': 1, 'film5': 2, 'film12': 4})
===================current user===================
{'film2': 3, 'film8': 4, 'film9': 3, 'film10': 5}
==========most similar user and his film==========
user4:{'film2': 5, 'film4': 2, 'film3': 3, 'film12': 3, 'film9': 1, 'film8': 2}
=================recommended film=================
film12
二、集合
1、创建:
>>> s={20,30,80}
>>> s
{80, 20, 30}
>>> x={20,60,[4]}#元素需是不可变的
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: unhashable type: 'list'
>>> x=set()#创建空集合
>>> set([(1,3),(3,),{5}])
Traceback (most recent call last):#集合不能作为集合的元素
File "<stdin>", line 1, in <module>
TypeError: unhashable type: 'set'
2、转换
>>> set('Hello China')
{'h', 'i', ' ', 'e', 'n', 'o', 'C', 'a', 'l', 'H'}
>>> a=set([0,1,2,3,0,85,6,3,8,2])#转化时自动去除重复元素
>>> a
{0, 1, 2, 3, 6, 8, 85}
3、增加
>>> s.add(40)
>>> s
{80, 40, 20, 30}
>>>
>>> s.update(30,100)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: 'int' object is not iterable
>>> s.update({30,100})
>>>
>>> s
{80, 20, 100, 40, 30}
4、删除
pop(),随机删除并返回一个元素,为空抛异常
remove(),删除指定元素,不存在抛异常
discard():删除指定元素,不存在忽略
>>> s
{80, 20, 100, 40, 30}
>>>
KeyboardInterrupt
>>> s.pop()
80
>>> s
{20, 100, 40, 30}
>>>
5、运算
>>> s
{20, 100, 40, 30}
>>> w={15,48,74,54,26}
>>> s | w #并
{100, 40, 74, 15, 48, 20, 54, 26, 30}
>>> s & w #交集
set()
>>> s-w #差集
{100, 40, 20, 30}
>>> s^w
{74, 15, 20, 26, 30, 100, 40, 48, 54} #对撑差集
>>>
6、应用
输出小于自然数下的所有素数
n=int(input('请输入一个自然数:'))
number=set(range(2,n))
#最大数的平方根
m=int(n**0.5)+1
#遍历最大整数平方根之内的自然数
for p in range(2,m):
for i in range(2,n//p):
#在集合中删除数字p的所有倍数
number.discard(i*p)
print(number)