1. 实例: 下载一首英文的歌词或文章,将所有,.?!等替换为空格,将所有大写转换为小写,统计某几个单词出现的次数,分隔出一个一个的单词。
      str='''Where is the father?
      Two brothers were looking at some beautiful paintings.
      "Look," said the elder brother. "How nice these paintings are!"
      "Yes," said the younger, "but in all these paintings there is only the mother and the children.
      Where is the father?"
      The elder brother thought for a moment and then explained, "Obviously he was painting the pictures."'''
      
      print('原文:'+str+'\n')
      
      str=str.lower()
      print('所有大写转换为小写:'+str+'\n')
      
      for i in ',.?!"':
          str=str.replace(i,' ')
      print('所有分隔符替换为空格:'+str+'\n')
      
      print('分割出所有单词:',str.split(' '),'\n')
      
      print('统计单词father出现次数:',str.count('father'))

    2. 列表实例:由字符串创建一个作业评分列表,做增删改查询统计遍历操作。例如,查询第一个3分的下标,统计1分的同学有多少个,3分的同学有多少个等。
      grade=list('231213212123')
      
      print('输出列表:',grade)
      print('输出列表长度:',len(grade))
      print('输出列表最大值:',max(grade))
      print('输出列表最小值:',min(grade))
      
      grade=[int(x) for x in grade]
      print('更改为数值型:',grade)
      
      grade.append(4)
      grade.insert(2,4)
      print('输出增加后列表:',grade)
      
      grade.pop()
      grade.pop(2)
      print('输出删除后列表:',grade)
      
      print('输出第一个3分的下标:',grade.index(3))
      
      print('统计1分的同学人数:',grade.count(1))
      
      print('统计3分的同学人数:',grade.count(3))
      
      grade.sort()
      print('输出排序后列表:',grade)

    3. 简要描述列表与元组的异同。

                   异:list是一种有序的序列,正向递增、反向递减序号;可以随时添加和删除其中的元素;没有长度限制、元素类型可以不同。

                          tuple和list非常类似,但是tuple一旦初始化就不能修改。

                   同:list和tuple是内置的有序集合,一个可变,一个不可变。

    

 

 

posted on 2017-09-22 13:09  077吴文欣  阅读(130)  评论(0编辑  收藏  举报