2018.09.20 作业三

本文展示了如何对英文歌词进行词频统计,并详细介绍了列表、元组、字典和集合等数据结构的基本操作,包括遍历、插入、删除、排序和统计等,通过具体代码实例,帮助读者理解Python中数据结构的应用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 

1.英文词频统计:

strBig = ('''Big Big World

Emilia

I'm a big big girl

In a big big world

It's not a big big thing if you leave me

But I do do feel

that I too too will miss you much

Miss you much.

I can see the first leaf falling

It's all yellow and nice

It's so very cold outside

Like the way I'm feeling inside

I'm a big big girl

In a big big world

It's not a big big thing if you leave me

But I do do feel

that I too too will miss you much

Miss you much.

Outside it's now raining

And tears are falling from my eyes

Why did it have to happen

Why did it all have to end

I'm a big big girl

In a big big world

It's not a big big thing if you leave me

But I do do feel

that I too too will miss you much

Miss you much.

I have your arms around me ooooh like fire

But when I open my eyes

You're gone.

I'm a big big girl

In a big big world

It's not a big big thing if you leave me

But I do do feel

that I too too will miss you much

Miss you much.

I'm a big big girl

In a big big world

It's not a big big thing if you leave me

But I do feel that will miss you much

Miss you much.''')


strList = strBig.split()
print(len(strList),strList)

print(strBig.lower())#将所有大写转换为小写

strSet = set(strList)
print(len(strSet),strSet)

strDict = {}
for word in strSet:
    strDict[word] = strList.count(word)

print(strDict.items())
print(len(strDict),strDict)


print(strDict['big'])
print(len(strDict),strDict)


strListSort = strList.sort()
print(strListSort)
print(strList)

for word in strList:
    print(word,strList.count(word))

  运行结果:

 

 

 

2.列表,元组,字典,集合的遍历

 

classmates = ['Michael','Bob','Tracy','李三','Tracy',56]
new = ['Rose','Jack']
classmates.insert(2,'Tom')#插入
classmates.append('Jack')#追加
classmates.extend(new)#扩展
classmates.pop(-1)#
print(classmates)
classmatesSet = set(classmates)

classmatesSet.add('Jack')
classmatesSet.remove('Bob')
print(classmatesSet)

print(list('abcdef'),tuple('abcdef'))



classmates = ['Tracy','Bob','Tracy','李三','Tracy',56]
score = [85,90,50,60]
cs = dict(zip(classmates,score))
print(cs)


s = {1,2,3}
s.add(4)#增加
print(s)
s.remove(4)#删除
print(s)
s = set([1, 1, 2, 2, 3, 3])#生成
print(s)
#集合运算
s1 = set([1, 2, 3])
s2 = set([2, 3, 4])
print(s2-s1)

 

  运行结果:

 

 

转载于:https://www.cnblogs.com/LinYxin/p/9679952.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值