6.00 Introduction to Computer Science and Programming Lec 9: Set

这个lec的课后补充材料里面有关于Python中Set的介绍,而这正是我所关心的,就单写一下。参考文献:http://docs.python.org/2/library/stdtypes.html#set-types-set-frozenset


Python中的Set:set和frozenset

set是一个无序的集合,集合中的元素是hashable的。常用的方法包括添加、删除元素,查询元素是否在集合中以及交、并等集合运算。


和其他的集合类一样,set支持 x in set, len(set)和for x in set。Python中内置两种set的类型:set和frozenset。其中,set是mutable的,可以进行元素的添加、删除等操作。mutable的元素就不能hashable,因此,set不能作为dict的key,也不能存储于其他set中。与之对应的,是immutable的frozenset。


在python2.7中,python可以使用这种方式来创建set:

set( {'jack', 'sjoerd'} )
frozenset( {'jack', 'sjoerd'} )



原文地址:https://www.cnblogs.com/jubincn/p/3381125.html