一.英文词频统计:
代码如下:
def music(n): a=n.lower() a=a.strip() for i in ‘‘‘,.!?;‘‘‘: a=a.replace(i,‘ ‘) a=a.split() dic={} for j in set(a): dic[j] = a.count(j) li = list(dic.items()) print(li) return if __name__ == ‘__main__‘: str=‘‘‘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‘‘‘ music(str)
结果如下:
[(‘is‘, 2), (‘believe‘, 2), (‘forever‘, 1), (‘to‘, 2), (‘more‘, 2), (‘life‘, 1), (‘till‘, 1), (‘stay‘, 1), (‘wherever‘, 2), (‘way‘, 1), (‘loved‘, 1), (‘one‘, 3), (‘in‘, 5), (‘there‘, 1), (‘true‘, 1), (‘safe‘, 1), (‘time‘, 2), (‘can‘, 1), (‘always‘, 1), (‘a‘, 1), (‘my‘, 9), (‘across‘, 1), (‘us‘, 2), (‘have‘, 1), (‘come‘, 1), (‘far‘, 3), (‘dreams‘, 1), (‘spaces‘, 1), (‘hold‘, 1), (‘fear‘, 1), (‘see‘, 1), (‘distance‘, 1), (‘know‘, 2), (‘go‘, 11), (‘door‘, 2), (‘never‘, 1), (‘feel‘, 1), (‘for‘, 1), ("there‘s", 1), (‘how‘, 1), ("we‘re", 1), (‘was‘, 1), (‘are‘, 3), (‘away‘, 1), (‘between‘, 1), (‘will‘, 5), (‘love‘, 3), ("you‘re", 3), (‘last‘, 1), (‘let‘, 1), (‘the‘, 5), (‘open‘, 2), (‘you‘, 11), ("we‘ll", 2), (‘some‘, 1), (‘heart‘, 9), (‘that‘, 5), (‘show‘, 1), (‘nothing‘, 1), (‘not‘, 1), (‘this‘, 1), (‘on‘, 12), (‘does‘, 2), (‘touch‘, 1), (‘here‘, 3), (‘when‘, 1), (‘i‘, 9), (‘lifetime‘, 1), (‘every‘, 1), (‘night‘, 1), (‘once‘, 2), (‘near‘, 2), (‘and‘, 12)]
二.练习:
1.总结列表,元组,字典,集合的联系与区别。
列表List用[,,]表示,可以重复,类型可以不同 ,是一组任意类型的值,按照一定顺序组合而成的。
元组tuple用(,,)表示,元组是有序的、只读的、不能修改。操作速度比列表快。
字典dict用{键-值},{key-value}表示,是无序的,字典最外面用大括号,每一组用冒号连起来,然后各组用逗号隔开。
key值是不能变的,list不能作为key,字符串、元祖、整数等都可以。字典最大的价值是查询,通过键,查找值。
集合是一组key的集合,但不存储value,并且key不能重复 。创建一个set,需要提供一个list作为输入集合,s = set([1,2,3])。它自动去掉重复值,没有add,remove方法。
2.列表,元组,字典,集合的遍历。
1)列表的遍历:
代码如下:
classmates=[‘Michael‘,‘Bob‘,‘Tracy‘,‘李三‘,‘Tracy‘,56] print(classmates) a=classmates[1] print("取元素片段:"+a) b=classmates[-1] print(b) print(classmates[1:3]) f=len(classmates) print(f) c=classmates.index(‘Tracy‘) print(c) d=classmates[1]=‘Sarah‘ print("修改为:"+d) e=classmates.count(‘Tracy‘) print(e) print(classmates) h=classmates.insert(2,"Jack") print(h) print(classmates) i=classmates.append("Adam") print(i) print(classmates) j=classmates.pop(1) print(j) print(classmates)
结果如下:
修改为:Sarah 2 [‘Michael‘, ‘Sarah‘, ‘Tracy‘, ‘李三‘, ‘Tracy‘, 56] None [‘Michael‘, ‘Sarah‘, ‘Jack‘, ‘Tracy‘, ‘李三‘, ‘Tracy‘, 56] None [‘Michael‘, ‘Sarah‘, ‘Jack‘, ‘Tracy‘, ‘李三‘, ‘Tracy‘, 56, ‘Adam‘] Sarah [‘Michael‘, ‘Jack‘, ‘Tracy‘, ‘李三‘, ‘Tracy‘, 56, ‘Adam‘]
2)元组的遍历:
代码如下:
c1=(‘Micheal‘,‘Bob‘,‘Tracy‘,‘李三‘,‘Tracy‘,56) print(len(c1)) a=tuple(‘abcde‘) print(a)
结果如下:
6 (‘a‘, ‘b‘, ‘c‘, ‘d‘, ‘e‘)
3)字典的遍历:
代码如下:
classmates=[‘Michael‘,‘Bob‘,‘Tracy‘] scores=[65,75,85] d={‘Bob‘:75,"Michael":65,‘Tracy‘:85} print(d[‘Michael‘]) print(len(d)) ‘Thomas‘ in d d[‘Rose‘]=88 print(d.pop(‘Bob‘)) print(d.keys()) print(d.values()) print(d.items())
结果如下:
65 3 75 dict_keys([‘Michael‘, ‘Tracy‘, ‘Rose‘]) dict_values([65, 85, 88]) dict_items([(‘Michael‘, 65), (‘Tracy‘, 85), (‘Rose‘, 88)])
4)集合的遍历:
s={1,2,3} s.add(4) s.remove(3) print(s) s=set([1,1,2,2,3,4]) print(s) s1= set([1,2,3]) s2= set([2,3,4]) print(s1,s2) print(s1 & s2) print(s1 | s2) print(s2 - s1)
结果如下:
{1, 2, 4} {1, 2, 3, 4} {1, 2, 3} {2, 3, 4} {2, 3} {1, 2, 3, 4} {4}
原文:https://www.cnblogs.com/aojt/p/9753477.html