python 9.21作业

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

>>> can=list('1123232323123')
>>> can
['1', '1', '2', '3', '2', '3', '2', '3', '2', '3', '1', '2', '3']
>>> can.append(4)
>>> can
['1', '1', '2', '3', '2', '3', '2', '3', '2', '3', '1', '2', '3', 4]
>>> can.insert(1,'5')
>>> can
['1', '5', '1', '2', '3', '2', '3', '2', '3', '2', '3', '1', '2', '3', 4]
>>> can.pop(1)
'5'
>>> can
['1', '1', '2', '3', '2', '3', '2', '3', '2', '3', '1', '2', '3', 4]
>>> can.index('3')
3
>>> can.count('1')
3
>>> can.count('3')
5

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

>>> dic=dict(zip(['1','2','3'],[92,94,96]))
>>> dic
{'1': 92, '2': 94, '3': 96}
>>> dic1={4:'98'}
>>> dic.update(dic1)
>>> dic
{'1': 92, '2': 94, '3': 96, 4: '98'}
>>> dic.pop(4)
'98'
>>> dic
{'1': 92, '2': 94, '3': 96}
>>> dic[4]=100
>>> dic
{'1': 92, '2': 94, '3': 96, 4: 100}
>>> dic.get('1')
92

 3  (1).列表,元组,字典,集合的遍历。

l=list('232345678')
t=set('789123454')
c={'a':'1','b':'2','c':'3'}
x=(1, 2, 3, 4, 5 )
print('列表的遍历:',end='')
for i in l:
    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='')

 (2).总结;列表,元组,字典,集合的联系与区别。

a.列表:列表是一些可以重复,类型不同的元素的一个清单这样子的一个东西,可读可修改,符号为[],可以使用append、pop、count等进行增删改计数操作等。

b.元组:和列表的不同之处在于只读不可修改,符号为()。

c.字典:字典里面存的是值对,有键和值得区分,符号为{}。

d.集合:可以通过set函数实现集合,集合的符号也是{}。

 

4  英文词频统计实例

A待分析字符串

B分解提取单词

       a大小写 txt.lower()

       b分隔符'.,:;?!-_'

C计数字典

D排序list.sort()

E输出TOP(10)

 

news = '''For years, British explorer William Lindesay’s inquiries about a possible
extension of the Great Wall in Mongolia turned up nothing, but the researcher
recently had a breakthrough. Seeking insight from Professor Baasan Tudevin, a
lauded but hard-to-find expert on the region, Lindesay posted an advertisement
in a local newspaper. It was a long shot, but the two connected and the Mongolian
geographer said he knew of several such structures in the Gobi desert, the Telegraph reports.
Lindesay formed an expedition in August and with two Land Cruisers, 44 gallons of water,
12 gallons of extra gasoline and a lead from Google Earth, began poking around about 25 miles
from the sensitive Chinese-Mongolian border. Two days into the exploration, his team
discovered what is thought to be the first section of the Great Wall to exist outside
of China. Lost for nearly 1,000 years, the wall’s 62-mile-long arm is made mostly of shrubs
and dirt. Lindesay told the Telegraph much of the wall is about shin-level, but there is
also a stretch that reaches up to his shoulders.'''

exc ={'','the','of','a','but','two','about','in','is'}
news = news.lower()
for i in ',.':
    news = news.replace(i,' ')
words = news.split(' ')
dic = {}
keys = set(words)
for w in exc:
    keys.remove(w)
for i in keys:
    dic[i]=words.count(i)
wc = list(dic.items())
wc.sort(key=lambda x:x[1],reverse=True)
for i in range(10):
    print(wc[i])

 

 

posted @ 2017-09-21 11:01  448钟瑞灿  阅读(163)  评论(0编辑  收藏  举报