Python学习记录(20)

1. 集合(set)集合的显著特点是:里面元素无重复

1)set的创建

>>> num2 = {1, 2, 4, 5, 6, 7, 5, 4, 3, 2, 1, 0}

>>> num2

{0, 1, 2, 3, 4, 5, 6, 7}

>>> set1 = set([1, 1, 2, 4, 5, 6])

>>> set1

{1, 2, 4, 5, 6}

2).add()   将一个参数添加进集合中

>>> num2 = {1, 2, 4, 5, 6, 7, 5, 4, 3, 2, 1, 0}

>>> num2.add(6.2)

>>> num2

{0, 1, 2, 3, 4, 5, 6, 7, 6.2}

3)  .remove()  将指定元素从原集合中删除

>>> num2 = {0, 1, 2, 3, 4, 5, 6, 7, 6.2}

>>> num2.remove(6.2)

>>> num2

{0, 1, 2, 3, 4, 5, 6, 7}

4)  .frozenset()  冰冻集合,集合不能改变

>>> num4 = frozenset([1, 2, 3, 4, 5, 3, 2, 1])

>>> num4

frozenset({1, 2, 3, 4, 5})

>>> num4.add(0)

Traceback (most recent call last):

  File "", line 1, in

    num4.add(0)

AttributeError: 'frozenset' object has no attribute 'add'

你可能感兴趣的:(Python学习记录(20))