0920第三次作业
练习一:
代码如下:
下载一首英文的歌词或文章str:
strlrc = ''' All I know is (ooh ooh ooh) We could go anywhere we could do Anything girl whatever the mood we're in All I know is (ooh ooh ooh) Getting lost late at night under stars Finding love standing right where we are your lips They pull me in the moment You and I alone and People may be watching I don't mind Cause anywhere with you feels right Anywhere with you feels like Paris in the rain, Paris in the rain We don't need a fancy town Or bottles that we can't pronounce Cause anywhere babe Is like Paris in the rain When I'm with you, when I'm with you Paris in the rain, Paris in the rain I look at you now and I want this forever I might not deserve it but there's nothing better Don't know how I ever did it all without you My heart is about to, about to jump out of my chest Feelings they come and they go, that they do Feelings they come and they go, not with you The late nights and street lights and the people Look at me girl and the whole world could stop Anywhere with you feels right Anywhere with you feels like Paris in the rain, Paris in the rain We don't need a fancy town Or bottles that we can't pronounce Cause anywhere babe Is like Paris in the rain When I'm with you, when I'm with you Paris in the rain, Paris in the rain Girl when I'm not with you All I do is miss you Come and set the mood right Underneath the moonlight Days in Paris, nights in Paris Paint you with my eyes closed Wonder where the time goes (Isn't it obvious, isn't it obvious?) So come and set the mood right Underneath the moonlight Anywhere with you feels right Anywhere with you feels like Paris in the rain, Paris in the rain Walking down an empty street Bottles underneath our feet'''
分隔出一个一个的单词 list:
strList = strlrc.split() print(strList)
统计每个单词出现的次数 dict:
strSet = set(strList) strDict = {} for word in strSet: strDict[word] = strList.count(word) print(len(strDict), strDict)
练习二:
总结列表,元组,字典,集合的联系与区别
列表,元组,字典,集合的联系与区别:
1.列表,元组是有顺序的,而字典和集合是没顺序的。列表是以[ ]形式表示,元组是以( )表示,字典以{ }表示,集合则是以[()]的形式表示。
2.列表是可变对象,可以有增删改操作,而元组是只读的,不能修改。字典使用键-值(key-value)存储,键是不可变的对象。
3.插入和查找速度快,不会随着键的增加而变慢,需要占用大量的内存。字典是用空间换取时间的一种方法。
4.集合是一个无序不重复元素集, 基本功能包括关系测试和消除重复元素。
列表,元组,字典,集合的遍历
列表是可变序列,元组是不可变序列;列表的值可以修改,而元祖的值初始化后不可修改,两者都是有序的。字典和集合 两者都是无序的,数据量大时可用集合或列表来创建