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

>>> d={'15':'85','16':'90','17':'95','18':'100'}

>>> d.pop('16')
'90'
>>> d
{'15': '85', '17': '95', '18': '100'}

>>> d['15']
'85'
>>> d['14']='69'
>>> d
{'15': '85', '17': '95', '18': '100', '14': '69'}

>>> d['15']='90'
>>> d
{'15': '90', '17': '95', '18': '100', '14': '69'}

 

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

 

元组和列表十分类似,只不过元组和字符串一样是不可变的,即你不能修改元组。元组是任意对象的有序集合,数组的同性。

列表是任意对象的有序集合;可通过偏移存取,注意,列表中的元素都是可变的,这是不同于元组的。

在字典里,不能再有序列操作,虽然字典在某些方面与列表类似,但不要把列表套在字典上。

集合才能做并、交集的运算。

 

#列表的遍历

>>> a=['asdf','asdf','fghd','sfdg']

>>> for i in a:

print(i)

asdf
asdf
fghd
sfdg

 

#元组的遍历

fruits=("apple","banana","orange")
for i in range(len(fruits)):
print(fruits[i])

 

#字典的遍历

fruit_dict = {'apple':1, 'banana':2, 'orange':3}  

for key in fruit_dict:  

print(fruit_dict[key])  

 

#集合的遍历

weekdays = set(['MON''TUE''WED''THU''FRI''SAT''SUN'])

for in weekdays:
    print (d)

 

3.英文词频统计实例

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

news='''The decision was announced by Ma Yun, Alibaba's chairman, during the World Xi'an Entrepreneurs Convention on Saturday, according to a press release given by Alibaba.

It cited Ma as saying that Xi'an is an important city in northwest China and the starting point of ancient Silk Road. Alibaba looks forward to be involved in the city's development.

Cainiao Network Technology, Alibaba's courier aggregator, will increase its investment in Xi'an to boost economic restructuring and transformation1 of northwestern areas, the press release said.

Aliyun, Alibaba's cloud computing2 subsidiary, and Xi'an Jiaotong University will establish a big data college to train talent, it said.

On Thursday, Alibaba said that its net profit jumped 96 percent to more than 14 billion yuan (2.1 billion U.S. dollars) year on year in the first fiscal3 quarter ending June. The group's revenue was about 50 billion yuan in the quarter, up 56 percent year on year.'''
news=news.lower()
for i in ',.()':
news=news.replace(i,' ')
words=news.split(' ')
print(words)

posted on 2017-09-22 09:55  016李云基  阅读(196)  评论(0编辑  收藏  举报