列表实例:由字符串创建一个作业评分列表,做增删改查询统计遍历操作。例如,查询第一个3分的下标,统计1分的同学有多少个,3分的同学有多少个等。
score=list('112233345611') print('分数为:') print(score) score.sort() print('排列后:') print(score) print('计数1:') print(score.count('1')) print('计数3:') print(score.count('3')) score.pop(5) print(score)
字典实例:建立学生学号成绩字典,做增删改查遍历操作。
>>> k={'201406114326':'3','201406114327':'2','201406114328':'1','201406114329':'0'} >>> k['201406114326'] '3' >>> k.pop('201406114327') '2' >>> k {'201406114326': '3', '201406114328': '1', '201406114329': '0'} >>> k.keys() dict_keys(['201406114326', '201406114328', '201406114329']) >>> k.values() dict_values(['3', '1', '0']) >>> k.items() dict_items([('201406114326', '3'), ('201406114328', '1'), ('201406114329', '0')]) >>> k.get('201406114326') '3' >>> k.get('201406114327','无结果') '无结果' >>>
列表,元组,字典,集合的遍历。
ls=list(range(1,10)) tu=tuple('123456789') d={'18':'99','44':'88','31':'77'} s=set('123456789') print('输出列表:') for i in ls: print(i,end=' ') print(' ') print('输出元组:') for i in tu: print(i,end=' ') print(' ') print('输出集合:') for i in s: print(i,end=' ') print(' ') print('输出字典:') for i in d: print(i,end=' ')
总结列表,元组,字典,集合的联系与区别。
列表:列表是有序的序列,并且可以随意更改长度,内容
元组:与列表非常相似,但列表内容、长度可以更改,元组不行
集合:集合是一些元素无序的组合。
英文词频统计实例
news='''President Xi Jinping has called for more systematic and innovative social governance, stressing the need to improve the capability to predict and prevent security risks. Xi, general secretary of the Communist Party of China Central Committee, was speaking on Tuesday at a Beijing conference to award individuals and units that have made outstanding contributions to the public security governance sector in the past five years. Since late 2012, people working in political and legal affairs have innovated social governance methods and dealt with large numbers of outstanding problems, making the general public feel safer, he said. The sense of security of the Chinese public has increased from 88 percent in 2012 to 92 percent in 2016, according to data released on Tuesday by the Central Committee for Comprehensive Management of Public Security, a central level authority in charge of social governance in China. However, Xi wants more effort to be taken to build a safer China. The authorities involved should be aware of the difficulties and challenges, carefully analyze the current situation of Chinese society, adopt technological innovation methods and improve the capacity to forecast and prevent risks, he said. Xi also stressed that social governance officers should have a better sense of the rule of law. More than 500 members from central and local political and law departments, as well as outstanding individuals and units, are participating in the two-day meeting, which concludes on Wednesday. "I'm excited and inspired after listening to the president's remarks, which made a comprehensive summary of social governance in the past five years and outlined the new requirements and tasks in the future," said Huang Ming, vice-minister of public security. Huang said the most valuable lesson he has learned is to go with the "innovative ideas and strategies to tackle the issues involving social security and win the support of the people". He Wenhao, a senior official in charge of political and legal affairs in the Tibet autonomous region who attended the conference, said the president's speech has built up his confidence in safeguarding Tibet. He said the biggest challenge he now faces is fighting "the separatist forces to ensure the continuous safety and stability in Tibet".''' news = news.lower() for i in ',.""?!': news = news.replace(i,' ') words = news.split(' ') s=set(words) dic={} for i in words: dic[i]=words.count(i) num = list(dic.items()) num.sort(key = lambda x:x[1],reverse=True) for i in range(10): print(num[i])
大小写 txt.lower(),分隔符'.,:;?!-_’
待分析字符串,分解提取单词
计数字典
排序list.sort()
输出TOP(10)