组合数据类型,英文词频统计
练习:
1.总结列表,元组,字典,集合的联系与区别。
列表 [,] 有序,可变,值可以重复
元组(,) 有序,不可修改,不可重复
集合可以用set()函数或者{}创建 用,分隔,不可有重复元素,是无序的。
字典由key和值values组成;可以用dict()函数或者{}创建,元素之间用“,”分隔,键与值之间用":"分隔;key键是唯一的、不可变的,值不要求,是无序的;用key来访问元素,是在集合的基础上的
2.列表,元组,字典,集合的遍历。
#列表的遍历
list=['Tracy','Amy','lucy',10]
print(list)
for a1 in list:
print(a1)
#元组的遍历
Tuple1=('gcjhj')
print(Tuple1)
for b1 in Tuple1:
print(b1)
#集合的遍历
Set1={2,4,6,8}
print(Set1)
for c in Set1:
print(c)
#字典的遍历
dict1 = {'a':55,'b':66,'c':77}
print(dict1)
for d in dict1:
print(d,dict1[d])

英文词频统计:
- 下载一首英文的歌词或文章str
- 分隔出一个一个的单词 list
- 统计每个单词出现的次数 dict
str2='''I will run, I will climb, I will soar
I'm undefeated
Jumpiing out of my skin, pull the chord
Yeah I believe it
The past, is everything we were
don't make us who we are
So I'll dream, until I make it real,
and all I see is stars
Its not until you fall that you fly
When your dreams come alive you're unstoppable
Take a shot, chase the sun, find the beautiful
We will glow in the dark turning dust to gold
And we'll dream it possible
possible
And we'll dream it possible
I will chase, I will reach, I will fly
Until I'm breaking, until I'm breaking
Out of my cage, like a bird in the night
I know I'm changing, I know I'm changing
In, into something big, better than before
And if it takes, takes a thousand lives
Then it's worth fighting for
Its not until you fall that you fly
When your dreams come alive you're unstoppable
Take a shot, chase the sun, find the beautiful
We will glow in the dark turning dust to gold
And we'll dream it possible
it possible
From the bottom to the top
We're sparking wild fire's
Never quit and never stop
The rest of our lives
From the bottom to the top
We're sparking wild fire's
Never quit and never stop
Its not until you fall that you fly
When your dreams come alive you're unstoppable
Take a shot, chase the sun, find the beautiful
We will glow in the dark turning dust to gold
And we'll dream it possible
possible
And we'll dream it possible'''.lower()
print(str2)
#aa = ''',."?!'''
#for word in aa:
#str2 =str2.replace('word',' ')
str2 =str2.replace('\n','')
str2 =str2.replace(",",'')
str2 = str2.strip()
str2 = str2.split()
print(str2)
print('统计每个单词出现的次数为:')
strSet=set(str2)
for word in strSet:
print(word,str2.count(word))
dict={} #单词计数字典
for word in str2:
dict[word] = str2.count(word)
print(len(dict),dict)

浙公网安备 33010602011771号