Python手册学习(二):集合

来源:互联网 发布:手机淘宝电脑版网页 编辑:程序博客网 时间:2024/06/05 00:39

1.集合的相关操作

>>> x=set('abcde')>>> y=set('bdxyz')>>> x{'d', 'b', 'e', 'a', 'c'}>>> y{'d', 'x', 'b', 'y', 'z'}>>> 'e'in xTrue>>> z=x.intersection(y)>>> z{'d', 'b'}>>> z.add('SPAM')>>> z{'d', 'b', 'SPAM'}>>> z.update(set(['x','y']))>>> z{'d', 'x', 'b', 'SPAM', 'y'}>>> z.remove('b')>>> z{'d', 'x', 'SPAM', 'y'}>>> S=set([1,2,3])>>> S{1, 2, 3}>>> S.union([3,4]){1, 2, 3, 4}>>> S.intersection((1,3,5)){1, 3}>>> S.issubset(range(-5,5))True

2、集合的解析形式

>>> {x**2 for x in [1,2,3,4]}{16, 1, 9, 4}>>> {x for x in 'spam'}{'m', 's', 'p', 'a'}>>> {c*4 for c in 'spamham'}{'aaaa', 'hhhh', 'pppp', 'ssss', 'mmmm'}

3、布尔值

>>> True == 1True>>> True is 1False>>> True + 45




 

0 0