练习:
总结列表,元组,字典,集合的联系与区别。
列表,元组,字典,集合的遍历
#列表,元祖,字典,集合的遍历#列表list1 = [1,2,3,4,5]for i in list1: print()#y元祖tuple = (10,23,65)for i in tuple: print(i)#=集合set = {85,56,55,33,48,65}for i in set: print(i)#字典
dict = {'金融':'1班','软件工程':'2班','信管':'3班'}for i in dict: print(i,dict[i])
结果如下:
英文词频统计:
- 下载一首英文的歌词或文章str
str = '''HeyHavana ooh na naAyyHalf of my heart is in Havana ooh na naAyy ayyHe took me back to East Atlanta na na naAll of my heart is in HavanaAyyThere's somethin' 'bout his mannersUh huhHavana ooh na naHe didn't walk up with that how you doin'When he came in the roomHe said there's a lot of girls I can do withBut I can't without youI'm doin' forever in a minuteThat summer night in JuneAnd papa says he got malo in himHe got me feelin' likeOooh oooh ooh I knew it when I met himI loved him when I left himGot me feelin' likeOooh oooh ooh and then I had to tell himI had to go oh na na na na naHavana ooh na naAyyHalf of my heart is in Havana ooh na naAyy ayyHe took me back to East Atlanta na na naAll of my heart is in HavanaAyyMy heart is in HavanaHavana ooh na naJefferyJust graduated fresh on campus mmmFresh out East Atlanta with no manners damnFresh out East AtlantaBump on her bumper like a traffic jamJamHey I was quick to pay that girl like uncle SamHere you go ayyBack it on me shawty cravin' on meGet to diggin' on meOn meShe waited on meThen whatShawty cakin' on me got the bacon on meWait upThis is history in the makin' on meOn mePoint blank close range that beIf it cost a million that's meThat's meI was gettin' mula man they feel meHavana ooh na naAyy ayyHalf of my heart is in Havana ooh na naOh ayy ayyHe took me back to East Atlanta na na naOh noAll of my heart is in HavanaAyyMy heart is in HavanaAyyHavana ooh na naOoh na na oh na na naTake me back back back likeOoh na na oh na na naTake me back back back likeOoh na na oh na na naTake me back back back likeOoh na na oh na na naTake me back back backYeah ayyOooh oooh oohOooh oooh oohTake me back to my HavanaHavana ooh na naHalf of my heart is in Havana ooh na naOh yeahHe took me back to East Atlanta na na naAll of my heart is in HavanaMy heart is in HavanaAyyHavana ooh na naUh huhOh na na naOh na na naHeyOh na na naNo no no take me backOh na na na'''strCount = str.count('na')print(strCount)s1 = str.startswith('Hey')print('是否以start开头')print(s1)s2 = str.endswith('na')print('是否以na结尾:')print(s2)s3 = str.isspace()print('是否为空')print(s3)s4 = str.isalnum()print('是否全为字母或数字:')print(s4)s5 = str.isalpha('')print('是否全字母:')print(s5)s6 = str.isdigit()print('是否全为数字')print(s6)s7 = str.islower()print('是否全为小写')print(s7)s8 = str.isupper()print(s8)
结果如下
-
:
- 分隔出一个一个的单词 list
#str每个单词分割组成列表形式str = ''''hanvana'''str = str.split()print(str)
- 结果
-
#计算单词出现的次数 strList = ('adigugyqiegyiuagiwgre') print("计算单词出现的次数:") strSet=set(strList) for word in strSet: print(word,strList.count(word))
结果如下:
-