Python Collection Set

Source: Internet
Author: User

Set set: mutable Sets and immutable sets

Mutable Collections: You can add a delete element to the collection, not a hash, a key that cannot be used as a dictionary, or an element of another collection.

Different elements are formed together to form a set, the collection does not record the position of the element or the insertion point, that is, the index index cannot be used to get the element.

Collection elements: Members that make up a collection cannot have duplicates, and duplicate filters are filtered.

Syntax: Set () function

The set (' list ') parameter requires a sequence

For example:

>>> s = Set (' Alex Li ')
>>> s
Set ([' A ', ' ', ' e ', ' I ', ' l ', ' X ']) here will find two L characters, output only once, one of the duplicate set to filter out.

>> a=[1,2,3,4,5,6]
>>> B=set (a) When the collection is created, the contents of the collection cannot be modified
>>> b
Set ([1, 2, 3, 4, 5, 6])

A collection object is an unordered set of hashed values: A collection member can be a dictionary key, a list, a dictionary, and these are non-hashed delegates.

How to view data in a collection:

1. View through for loop

2, through the way of iterators to see

Immutable Collections: You cannot add deleted elements to the collection, are not hashed, cannot be used as keys to the dictionary, and cannot be elements of other collections. Daily operations: 1. Create a collection: Because the collection does not have its own syntax format, it can only be created by using the collection's Factory method set () and Forzenset ().

>>> set ("[1,2,3,4,5]")
Set ([', ', ' 1 ', ' 3 ', ' 2 ', ' 5 ', ' 4 ', ' [', '] ')

2. Access collection: Because the collection itself is unordered, it is not possible to create an index or slice operation for the collection, only by looping through or using in and not, or judging the set elements.

>>> b
Set ([1, 2, 3, 4, 5, 6])
>>> 2 in B
True

>>> Li = [2,3, ' Alex ']
>>> Li
[2, 3, ' Alex ']
>>> B=set (LI)
>>> b
Set ([2, 3, ' Alex '])
>>> ' Alex ' in B
True
>>> ' ale ' in B through here you can find that ' Alex ' is as a whole in the collection, not as a string that can be matched separately.
False

3. Update the elements:

Add () method : Adds an element that considers the parameter as an element, regardless of the content of the argument.

>>> b
Set ([2, 3, ' Alex '])
>>> b.add (' Qiyuanchang ')
>>> b
Set ([2, 3, ' Alex ', ' Qiyuanchang '])

Update () method : The parameter is treated as a sequence, and the parameter is added to the collection by splitting it into a sequence, and repeated actions are done to redo it.

>>> b
Set ([2, 3, ' Alex ', ' Qiyuanchang ', ' uu ', ' u '])
>>> b.update (' Ops ')
>>> b
Set ([2, 3, ' Alex ', ' Qiyuanchang ', ' uu ', ' o ', ' P ', ' s ', ' U ']) here will be sent under the parameters ops was made split

>>> b.update (' wwwwww ')
>>> b
Set ([2, 3, ' Alex ', ' Qiyuanchang ', ' uu ', ' o ', ' P ', ' s ', ' U ', ' W ']) Here the repeated Watts are filtered out, leaving only one

>>> Li
[2, 3, ' Alex ']
>>> B=set (LI)
>>> b
Set ([2, 3, ' Alex '])
>>> b.update ([' + ', ' Qiyuanchang ']) when a parameter is a list, several elements in the list are split to add to the collection.
>>> b
Set ([' 2 ', 3, ' Alex ', ' Qiyuanchang '])

Remove () method : You can delete the specified element in the collection

>>> b
Set ([' 2 ', 3, ' Alex ', ' Qiyuanchang '])
>>> B.remove (2)
>>> b
Set ([' A ', 3, ' Alex ', ' Qiyuanchang '])

Pop () method : Randomly deletes the elements in the collection, and the test learns that the first collection element should be deleted by default.

>>> b
Set ([' A ', 3, ' Alex ', ' Qiyuanchang '])
>>> B.pop ()
' 12 '

Clear () method : Empty the collection

>>> B.clear ()
>>> b
Set ([])

del method : Delete Collection

>>> del s
>>> s
Traceback (most recent):
File "<stdin>", line 1, in <module>
Nameerror:name ' s ' is not defined

4. The type operator for the collection:

In, not in: You can tell if an element exists in the collection

>>> s=set (' Qiyuanchang ')
>>> s1=set (' Yuan ')
>>> s
Set ([' A ', ' C ', ' g ', ' I ', ' h ', ' n ', ' Q ', ' u ', ' Y '])
>>> ' Q ' in S
True

Sets are equivalent to not equal (= =,! =): Determine if two sets are the same

Subset, superset: Determines whether the element content of a collection is a subset of another collection.

>>> s=set (' Qiyuanchang ')
>>> s1=set (' Yuan ')
>>> S1 < s
True

Union: The union operation is actually equivalent to the or operation of the set, and the Union symbol has an equivalent method, Union ()

Intersection:

Subtraction

Python Collection 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.