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、英文词频统计实例
- 待分析字符串
- 分解提取单词
- 大小写 txt.lower()
- 分隔符'.,:;?!-_’
- 单词列表
- 单词计数字典
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]))