Python's Path to growth 6--set

Source: Internet
Author: User
Tags new set

I. What is set

  Set is an unordered, non-repeating sequence

Two. Create a set

(1) se={"123", "456"}

(2) li=[11,22,11,22]

Se=set (LI)

Then se={11,22}

Description: Set is a class, and the class name () is the __init__ method that executes the class.

Note: The second way to create a set representation in Python2 and Python3 is different.

1Admindemacbook-air-5: stu179471 admin$ python2Python 2.7.10 (default, Jul 14 2015, 19:46:27) 3[GCC 4.2.1 Compatible Apple LLVM 6.0 (clang-600.0.39)] on Darwin4Type" Help","Copyright","credits" or "License"  forMore information.5>>> li=[11,22,11,22]6>>> s1=set (LI)7>>>S18Set ([11, 22])9>>>exit ()TenAdmindemacbook-air-5: stu179471 admin$ python3 OnePython 3.5.1 (v3.5.1:37a07cee5969, Dec 5 2015, 21:12:44)  A[GCC 4.2.1 (Apple Inc. build 5666) (dot 3)] on Darwin -Type" Help","Copyright","credits" or "License"  forMore information. ->>> li=[11,22,11,22] the>>> s2=set (LI) ->>>S2 -{11, 22}
Python2 and Python3 differences

Three. Operation Set

1 S1.add () add an element2 S1.update (an iterative object) to add elements in bulk3 s1.difference (S2) exists in S1, and does not exist in S2, a new set is generated4 the intersection of s1.inetrsection (S2) S1 and S2 generates a new set5 s1.symmetric_difference (S2) S1 and S2 do not exist, symmetric difference sets, will generate a new set6 _update: The above three methods have their corresponding _update method (e.g.:d ifference_update ()), which does not generate a new set and is updated directly to S17 s1.union (S2) S1 and S28 s1.isdisjoint (S2) does not intersect, returns true9 s1.issubset (S2) S1 is a subset of S2 returns trueTen s1.issuperset (S2) S1 is the parent set of S2 returns True One Discard (Element) removes the specified element, no error is present A Remove (Element) ditto but not present will error -Pop () is randomly removed and gets the removed element

Four. Set small example

Implementation of the example shown

The code is as follows:

old_dict={    "#1": 8,    "#2": 4,    "#4": 2,}new_dict={    "#1": 4,    "#2": 4,    "#3": 2,}old_set=Set (Old_dict.keys ()) New_set=Set (New_dict.keys ()) Del_set=old_set.difference (new_set) Add_set=new_set.difference (old_set) Update_set=old_set.intersection (New_set) forIinchDel_set:old_dict.pop (i) forIinchAdd_set:old_dict[i]=New_dict[i] forIinchUpdate_set:old_dict[i]=New_dict[i]Print(old_dict)Print(new_dict)
Code

Five. Supplementary knowledge

  Many classes have double-underline methods, the double-underline method generally has a special meaning,

For example:

li=[1,2,3,4] #list __init__

Li () #list __call__

Li[0] #list __getitem__

Li[0]=1 #list __setitem__

  Del Li[0] #list __delitem__

Python's Path to growth 6--set

Related Article

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.