1、歌词分词并统计个数:
代码:
1 歌词 = """ 2 Yeah, you could be the greatest, you can be the best 3 是的,你可以成为最伟大的,你可以成为最好的 4 You can be the King Kong banging on your chest 5 你可以成为敲打着胸膛的金刚 6 You could beat the world, you could beat the war 7 你可以打败世界,你可以打败战争 8 You could talk to God, go banging on His door 9 你可以与上帝交谈,敲打他的大门 10 11 You can throw your hands up, you can beat the clock 12 你可以举起双手,你可以赶上时间 13 You can move a mountain, you can break rocks 14 你可以移动山峰,你可以击碎岩石 15 You could be a master, don‘t wait for luck 16 你可以成为主人,不需要等待幸运降临 17 Dedicate yourself and you can find yourself 18 奉献你自己,然后你便可以找到自我 19 20 Standing in the hall of fame 21 站在名人堂中 22 And the world‘s gonna know your name 23 全世界都会知道你的名字 24 Cause you‘re burning with the brightest flame 25 因为你为最明亮的火焰所燃 26 And the world‘s gonna know your name 27 全世界都会知道你的名字 28 And you‘ll be on the walls of the hall of fame 29 而你会位列名人堂之墙 30 31 You could go the distance, you can run the mile 32 你可以坚持到底,你可以跑到极限 33 You can walk straight through hell with a smile 34 你可以微笑着穿越地狱 35 You could be the hero, you could get the gold 36 你可以成为英雄,你可以获得金牌 37 Breaking all the records they thought never could be broke 38 打破所有那些别人认为牢不可破的记录 39 40 Do it for your people, do it for your pride 41 行动吧,为你的人民,为你的骄傲 42 Never gonna know if you never even try 43 如果不尝试就永远不会知道结果 44 Do it for your country, do it for you name 45 行动吧,为你的国家,为你的名誉 46 Cause there gonna be a day 47 因为将会有一天 48 49 When you‘re standing in the hall of fame 50 当你站在名人堂中 51 And the world‘s gonna know your name 52 全世界都会知道你的名字 53 Cause you‘re burning with the brightest flame、 54 因为你为最明亮的火焰所燃 55 And the world‘s gonna know your name 56 而全世界都会知道你的名字 57 And you‘ll be on the walls of the hall of fame 58 你会位列名人堂之墙 59 60 Be a champion, 61 成为冠军 62 Be a champion, 63 成为冠军 64 Be a champion, 65 成为冠军 66 Be a champion 67 成为冠军 68 On the walls of the hall of fame 69 在名人堂之墙上 70 71 Be students 72 成为学生 73 Be teachers 74 成为导师 75 Be politicians 76 成为政客 77 Be preachers 78 成为传教士 79 80 Be believers 81 成为信徒 82 Be leaders 83 成为领导者 84 Be astronauts 85 成为航天员 86 Be champions 87 成为冠军 88 Be truth seekers 89 成为真理的探索者 90 91 Be students 92 Be teachers 93 Be politicians 94 Be preachers 95 96 Be believers 97 Be leaders 98 Be astronauts 99 Be champions 100 101 Standing in the hall of fame 102 And the world‘s gonna know your name 103 Cause you‘re burning with the brightest flame 104 And the world‘s gonna know your name 105 And you‘ll be on the walls of the hall of fame 106 107 You could be the greatest, you can be the best 108 You can be the King Kong banging on your chest 109 You could beat the world, you could beat the war 110 You could talk to God, go banging on his door 111 112 You can throw your hands up, you can beat the clock 113 You can move a mountain, you can break rocks 114 You could be a master, don‘t wait for luck 115 Dedicate yourself and you can find yourself 116 117 Standing in the hall of fame 118 """ 119 120 歌词 = 歌词.split("\n") 121 # 删除空格 122 歌词 = list(x for x in 歌词 if x != "") 123 124 # 中英文分词 125 index1 = 歌词.index("成为真理的探索者") 126 英文部分 = [] 127 for index2 in range(0, index1+1): 128 if index2 % 2 == 0: # 英文行 129 英文部分.append(歌词[index2]) 130 131 英文部分 += 歌词[index1+1::] 132 133 # 拆分成单词 134 英文分词 = [] 135 # 先按逗号拆,再按空格拆 136 for i in 英文部分: 137 i = i.split(",") 138 for j in i: 139 英文分词.extend(j.split(" ")) 140 141 # 去空格 142 英文分词 = list(i.lower() for i in 英文分词 if i != "") 143 144 print(英文分词) 145 146 # 用字典,统计单词个数 147 set1 = set(英文分词) 148 KeyList = [] 149 ValueList = [] 150 151 for key in set1: 152 KeyList.append(key) 153 ValueList.append(英文分词.count(key)) 154 155 countTotal = dict(zip(KeyList, ValueList)) 156 indexList = [] 157 158 # 打印结果: 159 print(" 单词 出现次数".center(13)) 160 for key in countTotal.keys(): 161 print(key.center(13), countTotal[key])
截图:
2、列表元组集合字典的遍历
代码:
# 列表字典元祖的遍历 myList = list(range(10)) myTuple = tuple(range(10)) mySet = set(range(10)) myDict = {‘Bob‘: ‘80‘, ‘Tom‘: ‘100‘, ‘Mary‘: ‘90‘} for i in myList: print(i, end="") print() for j in myTuple: print(j, end="") print() for k in mySet: print(k, end="") print() for l in myDict.keys(): print(l, myDict[l])
截图:
3、列表元组集合字典的遍历
列表元组集合字典的联系与区别:
1、列表元组集合都是用于存储数据的,都可以进行数据的查找
2、列表集合字典都可以进行数据的增删改查
3、列表的增删改查的时间消耗随着元素的增加而增加,当元素大的时候比较浪费时间
4、元组只能查,不能增删改,可以保证数据的安全性,因为可操作性较低,资源占用也较少,适合用来存储经常查但不经常修改的数据
5、集合最重要的一个特性就是去重,适合用于存储不重复的key
6、字典存放的数据是key:value类型的,通过key可以非常快速的查找到对应的value.但是占用的内存和空间较大,是一种空间换时间的数据类型
列表元组集合字典的联系与区别:
1、列表元组集合都是用于存储数据的,都可以进行数据的查找
2、列表集合字典都可以进行数据的增删改查
3、列表的增删改查的时间消耗随着元素的增加而增加,当元素大的时候比较浪费时间
4、元组只能查,不能增删改,可以保证数据的安全性,因为可操作性较低,资源占用也较少,适合用来存储经常查但不经常修改的数据
5、集合最重要的一个特性就是去重,适合用于存储不重复的key
6、字典存放的数据是key:value类型的,通过key可以非常快速的查找到对应的value.但是占用的内存和空间较大,是一种空间换时间的数据类型
原文:https://www.cnblogs.com/traces2018/p/9684074.html