1、组合数据类型的区别:
列表:用"[]"创建每个元素之间用","隔开;是一种有序的序列,正向递增,反向递减序号,元素类型可以不一样,可以随时增加或删除元素.
元组:用"()"创建每个元素之间用","隔开;也可以进行索引,但是一旦创建了就不能修改里面的元素,不能删除其中的一个元素,用del可以删除整个元组:
字典:用"{}"创建每个键值对对之间用","隔开;键值队中键是唯一的,值可以取任何数据类型,不允许相同的键出现两次.
集合:用"{}"或者是set来创建,是无序不重复的序列,用remove来删除指定的元素
2、列表,元组,字典,集合的遍历:
#列表的遍历 myList=["小红","小李",80,90] print("列表的遍历:") for l1 in myList: print(l1) #元组的遍历 myTuple=(23,65,79,50) print("元组的遍历:") for T1 in myTuple: print(T1) #集合的遍历 mySet={43,58,90,74} print("集合的遍历:") for S1 in mySet: print(S1) #字典的遍历 classmate=["张三","李四","小虎"] sorce=[89,76,90] d={} print("字典的遍历:") d=dict(zip(classmate,sorce)) for i in d.keys(): print(i,d[i])
英文词频统计:
#英文歌词: str1='''I will not make the same mistakes that you did I will not let myself cause my heart so much misery I will not break the way you did You fell so hard I learned the hard way, to never let it get that far - Because of you I never stray too far from the sidewalk Because of you I learned to play on the safe side So I don't get hurt Because of you I find it hard to trust Not only me, but everyone around me Because of you I am afraid - I lose my way And it's not too long before you point it out I cannot cry Because I know that's weakness in your eyes I'm forced to fake a smile, a laugh Every day of my life My heart can't possibly break When it wasn't even whole to start with - Because of you I never stray too far from the sidewalk Because of you I learned to play on the safe side So I don't get hurt Because of you I find it hard to trust Not only me, but everyone around me Because of you I am afraid - I watched you die I heard you cry Every night in your sleep I was so young You should have known better than to lean on me You never thought of anyone else You just saw your pain And now I cry In the middle of the night Over the same damn thing - Because of you I never stray too far from the sidewalk Because of you I learned to play on the safe side so I don't get hurt Because of you I tried my hardest just to forget everything Because of you I don't know how to let anyone else in Because of you I'm ashamed of my life because it's empty Because of you I am afraid - Because of you''' #把单词全部变成小写 s1=str1.lower() print(s1) #去掉空格 str1=str1.lstrip() print(str1) #将歌词的每个单词分隔组成列表形式 print("将歌词的每个单词分隔组成列表形式:") strList=str1.split() print(strList) #计算单词出现的次数 print("计算单词出现的次数:") strSet=set(strList) for word in strSet: print(word,strList.count(word))