格式化字符串
#用split分隔字符串 st = 'https://www.taobao.com/market/peishi/shipin.php?spm=a217x.7278569.a214d69.11.73c050a3pItX7C' print(st.split('/')) classmate = ['Michael','Bob','Tracy','李三','Tracy'] print(classmate[1]) print(classmate[-1]) print(classmate[1:3]) print(len(classmate)) print(max(classmate)) print(min(classmate)) print(classmate.index('Tracy')) print(classmate.count('Tracy')) classmate[1] = 'Sarah' classmate.sort() classmate.insert(1,'jack') classmate.append('Adam') #list.extend(classmate) classmate.pop() classmate.pop(1)
2、
对taylor歌词进行分词
str = '' '''open打开歌词数据,加入str''' with open('d:/style.txt','r') as f: for line in f.readlines(): str += line.strip() '''用for循环+replace去除标点符号''' for old in ',,.。??!!': str.replace(old,' ') str = str.lower()#转换大小写 music = str.split(' ')#按空格划分 dic = {} #统计词频 for i in set(music): dic[i] = music.count(i) print(dic)