Python Collection Set () add Delete, intersect, set, set operation details

Source: Internet
Author: User

In Python set is a collection type of the base data type, which has both a mutable set (set ()) and an immutable set (Frozenset). Creating Set Sets, collection set additions , collection deletions , intersections , sets , and difference sets is a very useful approach.

Create Collection Set

The Python set class is in Python's sets module, and you are now using the python2.7.x that you do not need to import sets modules to create the collection directly.
>>>set (' Boy ')
Set ([' Y ', ' b ', ' O '])

Collection Add, delete

There are two common methods for adding Python collections, namely add and update.
Collection Add method: The element to be passed in as an entire add to the collection, for example:
>>> a = set (' Boy ')
>>> a.add (' python ')
>>> A
Set ([' Y ', ' python ', ' B ', ' O '])
Collection Update method: Splits the element to be passed in as an individual into the collection, for example:
>>> a = set (' Boy ')
>>> a.update (' python ')
>>> A
Set ([' B ', ' H ', ' o ', ' n ', ' P ', ' t ', ' Y '])

Collection Delete Action method: remove
Set ([' Y ', ' python ', ' B ', ' O '])
>>> a.remove (' python ')
>>> A
Set ([' Y ', ' b ', ' O '])

Python set () Set operation symbols, mathematical symbols

The intersection of the collection, the collection (and set), and the difference set, to understand the Python collection set and list of these very useful features, you should first understand some of the collection operation symbols



A simple demonstration of the concept of the difference set, intersection, and collection:

The variable collection python set is the www.iplaypy.com Python learning communication platform, which is more accessible in early Python learning. An iterative pair of images such as lists, dictionaries, and strings can be used as parameters to a collection. Set sets are unordered and cannot be manipulated by indexes and slices.

Python Collection Set () add Delete, intersect, set, set operation details

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.