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

本文通过实例展示了Python中字典的基本操作,包括增删改查及遍历,并提供了一个英文文本的词频统计案例。从创建字典到处理实际文本数据,读者可以学习如何有效地使用Python进行数据管理和文本分析。

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

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

d={'01':99,"02":88,"03":87,"04":76,"07":71,"06":92}
print(d["01"])
print("08" in d)
print(d.keys())
print(d.values())
print(d.items())
print(d.pop("03"))
print(d.get("05","查无此人"))
del(d["06"])
print(d)

 

 

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

ss=set("629130123")
sa=set("8756")
print(ss&sa)
print(ss|sa)
print(ss-sa)

for i in ss:
print(i,end='')
print()

lis=list("62918729")
tu=tuple("njdllkla")
dt=dict(zip(lis,tu))

for i in lis:
print(i,end='')
print()

for i in tu:
print(i,end='')
print()

for i in dt:
print(i,dt[i])

 

3、英文词频统计实例

  1. 待分析字符串
  2. 分解提取单词
    1. 大小写 txt.lower()
    2. 分隔符'.,:;?!-_’
    3. 单词列表
  3. 单词计数字典

news='''

 For Chinese people, eating mooncakes with families is one of the important ways to celebrate the Mid-Autumn Festival, which falls on Oct 4 this year, or the 15th day of the 8th Chinese lunar month.

As mass-produced mooncakes sweep market with different flavors and various stuffing, customers are inclined to turn to traditional taste, which makes hand-made mooncakes a hot commodity in the market.

Wei Quanfa's hand-made mooncake is one such case. The 72-year-old baker inherited the traditional craft from his family and started his new business in Zhengzhou, Central China's Henan province.

He carries out every procedure by himself, including making stuffing and the mooncake mould, boiling oil, and baking. Because of that, Wei is able to make no more than 40 kg a day in the last month before the festival, and a 30-centimeter-diameter mooncake is priced at over 200 yuan ($30).

With the festival just around the corner, reserving Wei's mooncakes even two days in advance is next to impossible.'''

 

for i in ",.()-$":
news=news.replace(i," ")

news=news.lower()
news=news.split(" ")
words=set(news)
dt={}
dt["mooncakes"]=news.count("mooncakes")
dt["with"]=news.count("with")

for i in words:
dt[i]=news.count(i)


for i in dt:
print("{0:10}{1}".format(i,dt[i]))

 

转载于:https://www.cnblogs.com/silu666-666/p/7578343.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值