总结列表,元组,字典,集合的联系与区别。
1.
列表(list)
具有以下特点:
1.可以用list()函数或者方括号[]创建,元素之间用逗号’,‘’分隔。
2.列表的元素不需要具有相同的类型
3.使用索引来访问元素
4.可切片
元组
元组跟列表很像,只不过元组用小括号来实现(),这里要区分好和生成器的区别,两者都可用小括号来实现的
具有以下特点:
1.可以用tuple()函数或者方括号()创建,元素之间用逗号’,‘’分隔。
2.元组的元素不需要具有相同的类型
3.使用索引来访问元素
4.可切片
5.元素的值一旦创建就不可修改!!!!!(这是区别与列表的一个特征)
字典(Dictionary)
字典是另一种可变容器模型,且可存储任意类型对象。
具有以下特点:
1.元素由键(key)和值(value)组成
2.可以用dict()函数或者方括号()创建,元素之间用逗号’,‘’分隔,键与值之间用冒号”:”隔开
3.键必须是唯一的,但值则不必。值可以取任何数据类型,但键必须是不可变的,如字符串,数字或元组
4.使用键(key)来访问元素
集合(set)
具有以下特点:
1.可以用set()函数或者方括号{}创建,元素之间用逗号”,”分隔。
2.与字典相比少了键
3.不可索引,不可切片
4.不可以有重复元素
联系
- 列表,元组,字典是有顺序的,而集合是没顺序的
- 列表是以方括号形式表示,元组是以圆括号表示,字典以花括号表示,集合则是以[()]的形式表示
- 列表是可变对象,它支持在原处修改的操作.也可以通过指定的索引和分片获取元素。区别于元组,可动态增加,删除,更新。
- 元组和列表在结构上没有什么区别,唯一的差异在于元组是只读的,不能修改。元组用“()”表示。元组一旦定义其长度和内容都是固定的。一旦创建元组,则这个元组就不能被修改,即不能对元组进行更新、增加、删除操作。若想创建包含一个元素的元组,则必须在该元素后面加逗号“,”,否则创建的不是一个元组,而是一个字符串。
#coding=utf-8 # anthor: Vitan # 列表的遍历 list=['a','b',123] for li in list: print(li) print('===============') # 元组的遍历 tu=('a','b',123) for t in tu: print(t) print('===============') # 字典遍历 dict={'a':'1','b':'2','c':'3'} for dic in dict: print(dic) print('===============') # 集合的遍历 set = set(['a','b','c','d']) for par in set: print(par)
1.
one = list('hello world!') for i in one: print(i) two = tuple('2379123') print(two) for j in two: print(j) three= set('abcdefg') print(three) for k in three: print(k) four= {'yes':100,'ok':60,'fit':80} print(four) for l in four: print(l,four[l])
2.英文词频
import string song='''Some dreams are big, some dreams are small Some dreams are carried away on the wind and never dreamed at all Some dreams tell lies, some dreams come true I've got a whole lot of dreams and I can dream for you If not for me, if not for you I'd be dreaming all day I wouldn't know what to do I'd hang around, I'd lose my way I'd put off what I couldn't do for another day Some dreams are big, some dreams are small Some dreams are carried away on the wind and never dreamed at all Some dreams tell lies, some dreams come true I've got a whole lot of dreams and I can dream for you I've spent my life hung up on dreams I float along like a summer cloud or so it seems I get it wrong most things I do But I can write a song and this one's for you Some dreams are big, some dreams are small Some dreams are carried away on the wind and never dreamed at all Some dreams tell lies, some dreams come true I've got a whole lot of dreams and I can dream for you Some call it fate, some call it chance Some call waiting around for someone to ask you to dance If I had my way, if I could call the tune You wouldn't have to wait, because I would dance you around the moon Some dreams are big, some dreams are small Some dreams are carried away on the wind and never dreamed at all Some dreams tell lies, some dreams come true I've got a whole lot of dreams and I can dream for you I've got a whole lot of dreams and I can dream for you''' # 歌词 song = song.lower() #全部变成小写 song = song.replace(",",'') #去除符号 song = song.split() #分隔 print(song) strsong = list(song) #转为列表 print(len(strsong),strsong) #统计词频 strdict={} #单词计数字典 for word in strsong: strdict[word] = song.count(word) print(len(strdict),strdict) for word in strsong: #单词计数集合 strdict[word] = strsong.count(word) print(len(strdict),strdict) wordlist = list(strdict.items()) #转为列表 wordlist.sort(key=lambda x:x[1],reverse=True) print(strsong) for i in range(20): #前20 print(wordlist[i])