064字典集合作業
1.
d={‘001‘:‘88‘,‘002‘:‘84‘,‘003‘:‘89‘,‘004‘:‘86‘,‘005‘:‘82‘}
>>> d[‘002‘]
‘84‘
>>> d.get(‘003‘)
SyntaxError: unexpected indent
>>> d.get(‘003‘)
‘89‘
>>> d.pop(‘003‘)
‘89‘
>>> d.get(‘003‘)
>>> print(d.get(‘003‘))
None
>>> d
{‘001‘: ‘88‘, ‘002‘: ‘84‘, ‘004‘: ‘86‘, ‘005‘: ‘82‘}
>>> d.keys()
dict_keys([‘001‘, ‘002‘, ‘004‘, ‘005‘])
>>> d.values()
dict_values([‘88‘, ‘84‘, ‘86‘, ‘82‘])
>>> d.items()
dict_items([(‘001‘, ‘88‘), (‘002‘, ‘84‘), (‘004‘, ‘86‘), (‘005‘, ‘82‘)])
2.
s=list(‘2347555544‘)
tu=tuple(‘9876687879‘)
print(ls)
print(tu)
dc=dict(zip(ls,tu))
print(dc)
print(‘ls遍歷\n‘)
for i in ls:
print(i)
print(‘tu遍歷\n‘)
for i in tu:
print(i)
print(‘dict遍歷\n‘)
for i in dc:
print(i,dc[i])
列表裏面的數據是可變的,可增刪改插。但是元組裏面的數據是不可變的,只可增,不可刪改,字典的查找與插入速度快,但是占用內存,浪費多,不能排序;集合不能重復,不存儲values,創建集合需要提供一個列表作為輸入集合。
3.s=‘‘‘every night in my dreams
i see you, i feel you,
that is how i know you go on
far across the distance
and spaces between us
you have come to show you go on
near, far, wherever you are
i believe that the heart does go on
once more you open the door
and you‘re here in my heart
and my heart will go on and on
love can touch us one time
and last for a lifetime
and never let go till we‘re one
love was when i loved you
one true time i hold to
in my life we‘ll always go on
near, far, wherever you are
i believe that the heart does go on
once more you open the door
and you‘re here in my heart
and my heart will go on and on
there is some love that will not go away
you‘re here, there‘s nothing i fear,
and i know that my heart will go on
we‘ll stay forever this way
you are safe in my heart
and my heart will go on and on
‘‘‘
s=s.lower()
s=s.replace(‘\n‘,‘ ‘)
word=s.split(‘ ‘)
dic={}
keys=set(word)
for i in keys:
dic[i]=word.count(i)
print(dic)
064字典集合作業