Python Collection Set Add Delete, Intersect, set, set operation symbols

Source: Internet
Author: User

In Python, set set is one of the basic data types, and it has both mutable set (set) and immutable set (Frozenset). creating Set Sets, collection set additions , collection deletions , intersections , sets , and difference sets is a very useful approach.

1. Create a Collection

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

2. Collection Add, delete

There are two common methods for adding a collection, 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 '])

Traversal of the 3.SET collection

Traverse set:

Def iterset ():    s = set ([]) for    item in s:        print (item) for            I in enumerate (s):        print (i)
4.python set operation symbols, mathematical symbols
The intersection of the collection, the collection (and set), and the difference set, to understand these very useful functions of collection set, we should first understand some of the collection operation symbols
 
 
(This image is from the network)
A simple demonstration of the concept of the difference set, intersection, and collection:
 
 
set sets are unordered and cannot be manipulated by indexes and slices

Python Collection Set Add Delete, Intersect, set, set operation symbols

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.