组合数据类型,英文词频统计
组合数据类型
mystr='https://edu.cnblogs.com/campus/gzcc/14NetworkEngineering/homework/870' ls=mystr.split('/') print(mystr,ls) classmates = ['Michael', 'Bob', 'Tracy','李三' ,'Tracy',56] print(classmates.count('Tracy')) classmates.insert(1,'Jack') classmates.append('Rose') #classmates.extend(ls) #classmates.sort(classmates) print(classmates) classmates.pop(1) print(classmates) ls=list(range(0,50,5)) lsser=list('Michael') print(ls,lsser) classmatesTu=tuple(classmates) print(classmates) print(classmatesTu)
英语词频统计
perfectstr='''I found a love for me Darling just dive right in and follow my lead Well I found a girl beautiful and sweet I never knew you were the someone waiting for me Cause we were just kids when we fell in love Not knowing what it was I will not give you up this time But darling just kiss me slow your heart is all I own And in your eyes you're holding mine Baby I'm dancing in the dark with you between my arms Barefoot on the grass listening to our favourite song When you said you looked a mess I whispered underneath my breath But you heard it darling you look perfect tonight Well I found a woman stronger than anyone I know She shares my dreams I hope that someday I'll share her home I found a love to carry more than just my secrets To carry love to carry children of our own We are still kids but we're so in love fighting against all odds I know we'll be alright this time Darling just hold my hand be my girl I'll be your man I see my future in your eyes Baby I'm dancing in the dark with you between my arms Barefoot on the grass listening to our favourite song When I saw you in that dress looking so beautiful I don't deserve this darling you look perfect tonight Baby I'm dancing in the dark with you between my arms Barefoot on the grass listening to our favourite song I have faith in what I see Now I know I have met an angel in person And she looks perfect I don't deserve this You look perfect tonight''' #将标点符号,特殊符号转换为空格 perfectstr=perfectstr.replace(',','') perfectstr=perfectstr.replace('''''','') perfectstr=perfectstr.replace(':','') perfectstr=perfectstr.replace('.','') perfectstr=perfectstr.replace('?','') perfectstr=perfectstr.replace('!','') perfectstr=perfectstr.replace('\n','') #去掉空格 perfectstr=perfectstr.lstrip() print(perfectstr) #将全部变成小写化 a=perfectstr.lower() print(a) #将每个单词分隔 perfectstrList=perfectstr.split() print(len(perfectstrList),perfectstrList) #用set把字符串转换成集合 strSet=set(perfectstrList) print(strSet) #计算单词出现的次数 strDict={} for hello in strSet: strDict[hello] =perfectstr.count(hello) print(strDict,len(strDict))