码迷,mamicode.com
首页 > 其他好文 > 详细

064字典集合作业

时间:2017-09-22 22:33:34      阅读:223      评论:0      收藏:0      [点我收藏+]

标签:字典   value   data-   distance   lower   排序   forever   输入   red   

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字典集合作业

标签:字典   value   data-   distance   lower   排序   forever   输入   red   

原文地址:http://www.cnblogs.com/-064/p/7573343.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!