1、字典
d={'Tom':'01','Helen':'02','David':'03','Jim':'04'}增d['Wang']='05'删d.pop('Tom')改d['Tom']='06'查d['Tom']
2、集合
a=set('12345')b=set('56789')增a.add('6')删b.remove('9')并集a&b交集a|ba-b
3、英文词频统计实例
abc='''every night in my dreamsi see you, i feel you,that is how i know you go onfar across the distanceand spaces between usyou have come to show you go onnear, far, wherever you arei believe that the heart does go ononce more you open the doorand you're here in my heartand my heart will go on and onlove can touch us one timeand last for a lifetimeand never let go till we're onelove was when i loved youone true time i hold toin my life we'll always go onnear, far, wherever you arei believe that the heart does go ononce more you open the doorand you're here in my heartand my heart will go on and onthere is some love that will not go awayyou're here, there's nothing i fear,and i know that my heart will go onwe'll stay forever this wayyou are safe in my heartAND my heart will go on and on'''abc=abc.lower()for i in ',.': abc=abc.replace(i,' ')words=abc.split(' ')print(words)di{}words.sort()disc = set(words)for i in disc: di[i] = 0for i in words: di[i] = di[i]+1items = di.items()print(items)
3、总结列表,元组,字典,集合的联系与区别
列表查找和插入的时间随元素的增加而增加,占用空间小,浪费内存很少;列表可以有N个元素,元素的类型是任意的,与列表本身无关。字典的查找和插入速度极快,不会随key的增加而变慢,需要占用大量内存,内存浪费多。字典使用键—值存储名具有极快的查找速度。字典的key必须是不可变对象。Python的集合(set)和其他语言类似, 是一个无序不重复元素集, 基本功能包括关系测试和消除重复元素. 集合对象还支持union(联合), intersection(交), difference(差)和sysmmetric difference(对称差集)等数学运算.由于集合是无序的,所以,sets 不支持 索引, 分片, 或其它类序列(sequence-like)的操作。
4、列表,元组,字典,集合的遍历
列表l = list('23132111123132')元组t = tuple('1233211232123')字典d = dict(zip([1,2,3,4],[4,3,2,1]))集合s = set(l)for i in l:print(i)for i in t:print(i)for i in s:print(i)for i in d:print(i)