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

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

grades = list('123212132321213')
print(grades)
grades.append('4')
print(grades)
grades.pop(-1)
print(grades)
print(len(grades))
grades[0] = '4'
print(grades)
print(grades.count('1'))
print(grades.count('3'))
grades.sort()
print(grades)

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

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

adict = {'201406114110':'80', '201406114111':'85', '201406114112':'90'}

for key in adict.keys():print(key)

for value in adict.values(): print(value)

for item in adict.items():print(item)

print(adict['201406114112'])
adict['201406114112'] = '100'#修改

print(adict['201406114112'])

adict['201406114116'] = '60'#增加
print(adict['201406114116'])

for item in adict.items():print(item)

del adict['201406114116'] #删除

for item in adict.items():print(item)

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


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

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

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

#直接遍历  
fruit_list = ['apple','banana','orange']  
for fruit in fruit_list:  
    print(fruit)  
      
#借助range()函数进行遍历  
fruit_list = ['apple','banana','orange']  
for i in range(len(fruit_list)):  
    print(fruit_list[i])  

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

fruit_dict = {'apple':1, 'banana':2, 'orange':3}  
for key in fruit_dict:  
    print(fruit_dict[key])  

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


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

           

什么是列表呢?我觉得列表就是我们日常生活中经常见到的清单。比如,统计过去一周我们买过的东西,把这些东西列出来,就是清单。由于我们买一种东西可能不止一次,所以清单中是允许有重复项的。如果我们扩大清单的范围,统计我们过去一周所有的花费情况,那么这也是一个清单,但这个清单里会有类别不同的项,比如我们买东西是一种花费,交水电费也是一种花费,这些项的类型是可以使不同的。python的列表个跟清单的道理是一样的,特点就是:可重复,类型可不同。类型不同也是跟数组最本质的区别了。python里的列表用“[]”表示:  

  lst = ['arwen',123]
  print lst[0]
  print lst[1]

  lst[0] = 'weiwen'

  向list中添加项有两种方法:append和extend。append是向

  如:

  (2)元组

  元组和列表在结构上没有什么区别,唯一的差异在于元组是只读的,不能修改。元组用“()”表示,如:

  tup = ('arwen',123)
  print tup[0]
  print tup[1]

  (3)集合就是我们数学学的集合,没有什么特殊的定义。集合最好的应用是去重。集合没有特殊的表示方法,而是通过一个set函数转换成集合,如:

  lst = [ 1, 1, 0]

  lst_set = set( lst ) #lst_set 为1 , 0

  tup = (2, 2, 1)

  tup_set = set( tup) # tup_set为2 , 1

  for item in lst_set:

    print item

  (4)最后一个是字典。字典存储键值对数据,如:

  1:a,2:b,3:c

  字典最外面用大括号,每一组用冒号连起来,然后各组用逗号隔开。

  字典最大的价值是查询,通过键,查找值。


4.英文词频统计实例
     1待分析字符串
      2分解提取单词

               

news = '''When I was young I'd listen to the radio
 Waiting for my favorite songs
 When they played I'd sing along,
 It make me smile.
  
Those were such happy times and not so long ago
 How I wondered where they'd gone.
 But they're back again just like a long lost friend
 All the songs I love so well.
 Every shalala every wo'wo
 still shines.
  
Every shing-a-ling-a-ling that they're starting to sing
 so fine
  
When they get to the part
 where he's breaking her heart
 It can really make me cry
 just like before.
 It's yesterday once more.
 (Shoobie do lang lang)
 (Shoobie do lang lang)
 Looking bak on how it was in years gone by
 And the good times that had
 makes today seem rather sad,
 So much has changed.
  
It was songs of love that I would sing to them
 And I'd memorise each word.
 Those old melodies still sound so good to me
 As they melt the years away
 Every shalala every wo'wo still shines
  
Every shing-a-ling-a-ling that they're startingTo sing
 so fine
 All my best memorise come back clearly to me
 Some can even make me cry
 just like before.
 It's yesterday once more.
 (Shoobie do lang lang)
 Every shalala every wo'wo still shines.
 Every shing-a-ling-a-ling that they're starting to sing
 so fine
 Every shalala every wo'wo still shines.
 Every shing-a-ling-a-ling that they're starting to sing'''


                   1大小写 txt.lower()

                  

news = news.lower()


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

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


      4计数字典

dic = {}
keys = set(words)
for i in keys:
    dic[i] = words.count(i)


      5排序list.sort()

wc = list(dic.items())
wc.sort(key = lambda x:x[1],reverse = True)#函数定义


6输出TOP(10)

words = news.split(' ')
print(news,end=' ')
print(words,end=' ')

for i in range(10):
    print(wc[i])

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