##列表遍历 classmates = [‘Michael‘,‘Bob‘,‘Jack‘,‘Tracy‘,‘Tom‘,‘Lucy‘] print(classmates) classmatesList = classmates #列表的遍历 for word in classmatesList: print(word)
##元组遍历 classmatesT = (‘1‘,‘3‘,‘2‘,‘6‘,‘5‘,‘7‘) sTuple = tuple(classmatesT) for numb in sTuple: print(numb)
##字典遍历 sDict = {‘name1‘:‘Michael‘,‘name2‘:‘Jack‘,‘name3‘:‘Tom‘} for key in sDict: print(key,‘value:‘,sDict[key])
##集合遍历 classmatesS = ([‘Michael‘,‘Bob‘,‘Jack‘,‘Tracy‘,‘Tom‘,‘Lucy‘]) sSet = set(classmatesS) for name in sSet: print(name)
##英文词频统计 str = ‘‘‘i‘m a big big girl in a big big world it‘s not a big big thing if you leave me but i do do feel that i too too will miss you much miss you much... i can see the first leaf falling it‘s all yellow and nice it‘s so very cold outside like the way i‘m feeling inside i‘m a big big girl in a big big world it‘s not a big big thing if you leave me but i do do feel that i too too will miss you much miss you much... i‘m a big big girl in a big big world it‘s not a big big thing if you leave me but i do do feel that i too too will miss you much miss you much... i‘m a big big girl in a big big world it‘s not a big big thing if you leave me but i do do feel that i too too will miss you much miss you much... ‘‘‘ #下载一首英文歌词 strList = str.split() #分隔一个一个单词list print(strList) strDicr = { } ##统计每个单词出现的次数dict for word in strList: strDicr[word] = strList.count(word) print(strDicr)
列表,元组,字典,集合的联系与区别:
列表:[ ]可以进行的操作包括索引,切片,加,乘,检查成员,列表的数据项不需要具有相同的类型,因为列表有增删改的作用,所以速度会比元组快。
元组:()是存一组数据,只要一旦创建,便不能修改,所以又叫只读列表。只需要在括号中添加元素,并使用逗号隔开即可。
字典:{key,value}是另一种可变容器模型,且可存储任意类型对象。字典的每个键值对()用冒号(:)分割每个对之间用逗号(,)分割,整个字典包括在花括号({})中
集合 : ()是一个无序的,不可重复的,数据组合,可以去掉重复值,还可以进行关系测试
原文:https://www.cnblogs.com/XLxielin/p/9688619.html