组合数据类型练习,英文词频统计实例

本文详细介绍如何使用Python中的列表、元组、字典和集合进行数据操作,包括增删改查、遍历等基本操作,并通过具体示例展示了如何统计词频,适合初学者快速掌握Python数据结构的应用。

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

1、由字符串创建一个作业评分表,做增删改查询统计遍历操作,例如查询第一个3分的下标,统计1分的同学有几个,3分的同学有几个,增删改查等等。

>>> fenshu = list('1213223131312232323')
>>> fenshu
['1', '2', '1', '3', '2', '2', '3', '1', '3', '1', '3', '1', '2', '2', '3', '2', '3', '2', '3']
>>> fenshu.index('3')
3
>>> fenshu.count('1')
5
>>> fenshu.count('3')
7
>>> fenshu.append('1')
>>> fenshu
['1', '2', '1', '3', '2', '2', '3', '1', '3', '1', '3', '1', '2', '2', '3', '2', '3', '2', '3', '1']
>>> fenshu.insert(1,'3')
>>> fenshu
['1', '3', '2', '1', '3', '2', '2', '3', '1', '3', '1', '3', '1', '2', '2', '3', '2', '3', '2', '3', '1']
>>> fenshu.pop()
'1'
>>> fenshu.pop(3)
'1'
>>> fenshu
['1', '3', '2', '3', '2', '2', '3', '1', '3', '1', '3', '1', '2', '2', '3', '2', '3', '2', '3']
>>> 

2、字典实例:建立学生学号成绩字典,做增删改查遍历操作。

>>> k={'201406114326':'3','201406114327':'2','201406114328':'1','201406114329':'0'}
>>> k['201406114326']
'3'
>>> k.pop('201406114327')
'2'
>>> k
{'201406114326': '3', '201406114328': '1', '201406114329': '0'}
>>> k.keys()
dict_keys(['201406114326', '201406114328', '201406114329'])
>>> k.values()
dict_values(['3', '1', '0'])
>>> k.items()
dict_items([('201406114326', '3'), ('201406114328', '1'), ('201406114329', '0')])
>>> k.get('201406114326')
'3'
>>> k.get('201406114327','无结果')
'无结果'
>>> 

 3、列表,元组,字典,集合的遍历,总结列表,元组,字典,集合的联系与区别。

>>> fenshu=list('32123123123')
>>> zd=tuple('32123123123')
>>> k={'201406114326':'3','201406114327':'2','201406114328':'1','201406114329':'0'}
>>> s=set('32123123123')
>>> fenshu
['3', '2', '1', '2', '3', '1', '2', '3', '1', '2', '3']
>>> zd
('3', '2', '1', '2', '3', '1', '2', '3', '1', '2', '3')
>>> k
{'201406114326': '3', '201406114327': '2', '201406114328': '1', '201406114329': '0'}
>>> s
{'3', '2', '1'}
>>> for i in fenshu:
    print(i,end='')

    
32123123123
>>> for i in zd:
    print(i,end='')

    
32123123123
>>> for i in k:
    print(i)

    
201406114326
201406114327
201406114328
201406114329
>>> for i in s:
    print(i)

    
3
2
1
>>> 

 4.词频统计

news='''My father was a self-taught mandolin player.
He was one of the best string instrument players in our town.
He could not read music, but if he heard a tune a few times,
he could play it. When he was younger, he was a member of a small
country music band. They would play at local dances and on a few occasions
would play for the local radio station. He often told us how he had
auditioned and earned a position in a band that featured Patsy Cline as
their lead singer. He told the family that after he was hired he never
went back. Dad was a very religious man. He stated that there was a lot of
drinking and cursing the day of his audition and he did not
want to be around that type of environment. '''
news=news.lower()
for i in ',.':
    news=news.replace(i,' ')
words=news.split(' ')
dict={}
keys=set(words)
for i in words:
    dict[i]=words.count(i)
count=list(dict.items())

count.sort(key=lambda x:x[1],reverse=True)
for i in range(10):
    print(count[i])

转载于:https://www.cnblogs.com/ELsky/p/7568631.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值