数据组合 ;歌曲分词


数据组合
classmate=['niko','john','tracy','张三'] ##定义一个列表

print(classmate[2]) ##输出列表

print(len(classmate)) ##取长
print(max(classmate))
print(min(classmate))
print(classmate.index('john')) ##索引john 在哪个位置
print(classmate.count('tracy')) ##计数tracy 几个字符
classmate[1]='tracy' ##将列表中的john 改为tracy
print(classmate)
classmate.sort() ##没有返回值,改变列表本身
classmate.insert(1,'jack') ##在1的位置插入jack
print(classmate)
classmate.append('adem') ##再增加一个名字
print(classmate)

歌曲分词

songStr='''The snow glows white on the mountain tonight
Not a footprint to be seen
A kingdom of isolation
And it looks like I'm the queen
The wind is howling like this swirling storm inside
Couldn't keep it in, heaven knows I've tried
Don't let them in, don't let them see
Be the good girl you always have to be
Conceal, don't feel, don't let them know
Well, now they know
Let it go, let it go
Can't hold it back anymore
Let it go, let it go
Turn away and slam the door
I don't care what they're going to say
Let the storm rage on
The cold never bothered me anyway
It's funny how some distance makes everything seem small
And the fears that once controlled me can't get to me at all
It's time to see what I can do
To test the limits and break through
No right, no wrong, no rules for me
I'm free
Let it go, let it go
I am one with the wind and sky
Let it go, let it go
You'll never see me cry
Here I stand and here I'll stay
Let the storm rage on
My power flurries through the air into the ground
My soul is spiraling in frozen fractals all around
And one thought crystallizes like an icy blast
I'm never going back, the past is in the past
Let it go, let it go
And I'll rise like the break of dawn
Let it go, let it go
That perfect girl is gone
Here I stand in the light of day
Let the storm rage on
The cold never bothered me anyway
'''.lower() ##将歌词中的大写改为小写
songStr=songStr.replace(',','') ##将歌词的逗号用空格
songStr=songStr.split()##将歌词分隔出一个个单词
print(songStr)
print(songStr.count('let'))
for word in songStr: ##遍历每个词出现的次数
print(songStr.count(word))



##sep=''',.:?""'''
##for c in sep : 将所有的符号用空格代替
## songStr=songStr.replace(c,'')
type(songStr)


song =set(songStr)
print(song)
songDict={}
for da in song:
songDict[da]=songStr.count(da)
print(songDict,len(songDict))


结果:

 

补充:2018/10/15

girlset =set(songStr.split())
girldict ={}
for i in girlset:
girldict[i]=songStr.count(i)

for key in girldict:
print(key,girldict[key])

wcList =list(girldict.items())
print(wcList)

结果:

 

##(函数定义)

## def.takeSecond(elem):
##   return elem[20]
##wcList.sort(key=takeSecond,reverse=True)

##items.sort(key=lambda x:x[1],reverse=True) //匿名函数

wcList.sort(lambda x:x[20],reverse=True)
print(wclist)

结果:

 






posted @ 2018-10-08 11:38  澄枫一叶  阅读(372)  评论(0编辑  收藏  举报