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

本文通过具体实例介绍了如何使用Python进行列表、字典的操作,并对比了列表、元组、字典和集合的不同之处,最后通过一个英文词频统计实例展示了基本的数据处理技巧。

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

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

 1 work = list("1232122331231")
 2 print(work)
 3 
 4 work.append('3')
 5 print(work[-1])
 6 
 7 work.pop()
 8 print(work[-1])
 9 
10 work[-1] = 2
11 print(work[-1])
12 
13 print(work.index('3'))
14 print(work.count('1'))
15 print(work.count('3'))

输出结果:

['1', '2', '3', '2', '1', '2', '2', '3', '3', '1', '2', '3', '1']
3
1
2
2
3
4

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

score = {1:'44',
             2:'66',
             3:'78',
             4:'67',
             5:'88'
            }
print(score)
score[6] = '98'
print(score)
score[6] = '99'
print(score)
score.pop(6)
print(score)

for i in score:
    print("{:>2} : {:<2}".format(i,score.get(i)))

输出结果:

{1: '44', 2: '66', 3: '78', 4: '67', 5: '88'}
{1: '44', 2: '66', 3: '78', 4: '67', 5: '88', 6: '98'}
{1: '44', 2: '66', 3: '78', 4: '67', 5: '88', 6: '99'}
{1: '44', 2: '66', 3: '78', 4: '67', 5: '88'}
 1 : 44
 2 : 66
 3 : 78
 4 : 67
 5 : 88

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

s=list('456132798')
t=set('4561327489')
c={'小明':'1','小红':'3','小华':'2'}
x=(1, 2, 3, 4, 5 )
print('列表的遍历为:',end='')
for i in s:
    print(i,end='')
print()
print('元祖的遍历为:',end='')
for i in x:
    print(i,end='')
print()
print('字典key的遍历为:',end='')
for i in c:
    print(i,end='')
print()
print('字典value的遍历为:',end='')
for i in c.values():
    print(i,end='')
print()
print('数组的遍历为:',end='')
for i in x:
    print(i,end='')

输出结果:

列表的遍历为:456132798
元祖的遍历为:12345
字典key的遍历为:小明小红小华
字典value的遍历为:132
数组的遍历为:12345

4、英文词频统计实例

  1. 待分析字符串
  2. 分解提取单词
    1. 大小写 txt.lower()
    2. 分隔符'.,:;?!-_’
  3. 计数字典
  4. 排序list.sort()
  5. 输出TOP(10)
s='''Well I wonder could it be When I was dreaming about you baby
You were dreaming of me Call me crazy Call me blind To still be suffering
is stupid after all of this time Did I lose my love to someone better
And does she love you like I do I do, you know I really really do
Well hey So much I need to say Been lonely since the day The day you went away
So sad but true For me there's only you Been crying since the day
the day you went away.!?'''
print("do出现次数",s.count('do'))
s=s.replace(",","")
s=s.replace('.','')
s=s.replace('?','')
s=s.replace('!','')
s=s.replace('\n','')
s=s.lower()
s1=s.split(' ')
key=set(s1)
dic={}
for i in key:
    dic[i]=s.count(i)
wc=list(dic.items())
wc.sort(key=lambda x:x[1],reverse=True)
for i in range(10):
print(wc[i])

 

转载于:https://www.cnblogs.com/xypbk/p/7563498.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值