Liaoche PY Note 5 set

Source: Internet
Author: User

Set is similar to Dict and is a set of keys, but does not store value

>>> s = set([1, 2, 3])>>> s{1, 2, 3}

Note that the parameter passed in [1, 2, 3] is a list, and the display {1, 2, 3} only tells you that the set has 3 elements, the order of the display does not indicate that the set is ordered.

Repeating elements are automatically filtered in set:

>>> s = set([1, 1, 2, 2, 3, 3])>>> s{1, 2, 3}

You can add(key) add elements to a set by means of a method, and you can add them repeatedly, but with no effect:

>>> s.add(4)>>> s{1, 2, 3, 4}>>> s.add(4)>>> s{1, 2, 3, 4}

Repeating elements are automatically filtered in set:

>>> s = set([1, 1, 2, 2, 3, 3])>>> s{1, 2, 3}

You can add(key) add elements to a set by means of a method, and you can add them repeatedly, but with no effect:

>>> s.add(4)>>> s{1, 2, 3, 4}>>> s.add(4)>>> s{1, 2, 3, 4}



remove(key)You can delete an element by means of:

>>> s.remove(4)>>> s{1, 2, 3}



Set can be regarded as a set of unordered and non-repeating elements in mathematical sense, so two sets can do the intersection and set of mathematical meanings, and so on:

>>> s1 = set([1, 2, 3])>>> s2 = set([2, 3, 4])>>> s1 & s2{2, 3}>>> s1 | s2{1, 2, 3, 4}


set和dict的唯一区别仅在于没有存储对应的value,但是,set的原理和dict一样,所以,同样不可以放入可变对象

Liaoche PY Note 5 set

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.