Python data type--set (collection)

Source: Internet
Author: User

Blog Address: http://www.cnblogs.com/yudanqu/

First of all, a brief introduction to Set,set is what we learned in high school, when the nature of the collection includes a point where the collection cannot have duplicate numbers. We now use a more extensive collection, which can store characters and so on.

    • Set: Similar to Dict, is a set of keys that does not store value
    • Nature: A collection of unordered and non-repeating elements

 Perhaps I said that he is similar to Dict, for the first-class students do not understand, how can this be similar? Let's see:

  If there is anything to find, the dictionary (dict) and set (set) are expressed in curly braces, and that is the essence of him, so look down first.

1. Create

(1) Create set requires a list or tuple or dict as the input set
(2) Where duplicate elements are automatically filtered in set

2. Add

S is a collection to which elements are added:

    • S.add (6) # Add a number 6 to it, and no effect if you add a repetition
    • S.add ([7,8,9]) # error because the list cannot be a mutable object as Key,list and Dict
    • # tuples can be added as objects because tuples are immutable objects
    • S.update () # can split the list tuple into set, including the string, and it will be opened to the collection
3. Delete
    • S.remove () # parameter for element inside, cannot be deleted by subscript, because itself unordered, no index
    • Set has no index, but can traverse output
    • It can also be used for Index,data in enumeration (s), which may appear to be indexed but not valid
4. Intersection
1 a1 = s1 & S2  #  to intersect and output new set
5, and set
1 # the assembly returns the new set

The *set itself is not commonly used, but can be used by his type conversion, using its non-repeating characteristics

Yu Tanku (yudanqu)

Blog Address: http://www.cnblogs.com/yudanqu/

Python data type--set (collection)

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.