Python---set

Source: Internet
Author: User

Python's set is a set of unordered, non-repeating elements, with basic functionality including relationship testing and De-duplication Elements. The collection objects also support and, intersection, difference, symmetry and so On.

Sets supports X in set, len (set), and for x in set. As an unordered collection, sets does not record the element position or insertion Point. therefore, sets does not support the operation of indexing, slicing, or other class sequences (sequence-like).

Back to Top

Basic Operations

650) this.width=650; "src=" http://common.cnblogs.com/images/copycode.gif "alt=" Copy Code "/>

>>> x = set ("jihite") >>> y = set ([' d ', ' i ', ' m ', ' i ', ' t ', ' e ']) >>> x #把字符串转化为set, go to reset Set ( [' i ', ' H ', ' J ', ' e ', ' t ']) >>> yset ([' i ', ' e ', ' m ', ' d ', ' t ']) >>> x & y #交set ([' i ', ' e ', ' t ']) >>> x | Y #并set ([' e ', ' d ', ' i ', ' H ', ' J ', ' m ', ' t ']) >>> x-y #差set ([' H ', ' J ']) >>> y-xset ([' m ', ' d ']) >& gt;> x ^ y #对称差: the intersection of X and y minus the set set ([' D ', ' H ', ' J ', ' M '])

650) this.width=650; "src=" http://common.cnblogs.com/images/copycode.gif "alt=" Copy Code "/>

Back to Top

function operation

650) this.width=650; "src=" http://common.cnblogs.com/images/copycode.gif "alt= "copy code"/>

>>> xset ([' i ',  ' H ',  ' J ',  ' e ',  ' t ']) >>> s = set ("hi") >>> sset ([' i ',  ' h ']) >>> len (x)                       #长度5 >>>  ' i '   In xtrue>>> s.issubset (x)                #s是否为x的子集True >>> yset ([' i ',  ' e ',  ' m ',  ' d ',  ' t ']) >>>  x.union (y)                   #交set ([' e ',  ' d ',  ' i ',  ' H ',  ' J ',  ' m ',  ' t ']) >>> x.intersection (y)           #并set ([' i ',  ' e ',  ' t ') >>>  X.difference (y)             #差set ([' H ',  ' J ']) > >> x.symmetric_diffErence (y)   #对称差set ([' d ',  ' H ',  ' J ',  ' m ']) >>> s.update (x)                  #更新s, plus the element >>> sset in X ([' E ',  ' t ',  ' I ',  ' H ',  ' J ']) >>> s.add (1)                     #增加元素 >>> sset ([1,  ' E ',  ' t ',  ' I ',  ' H ',  ' J ']) >>> s.remove (1)                  #删除已有元素, If no exception is returned >>> sset ([' e ',  ' t ',   ' I ',  ' H ',  ' J ']) >>> s.remove (2) traceback  (most recent call  last):  file  "<pyshell#29>", line 1, in <module>     s.remove (2) Keyerror: 2>>> s.discard (2)                  #如果存在元素, delete; no exception >>> sset ([' e ',  ' t ',  ' I ',  ' H ',  ' J ']) >>> s.clear ()                     #清除set >>> sset ([]) >>> xset ([' i ',  ' H ',  ' J ',   ' e ',  ' t ']) >>> x.pop ()                       #随机删除一元素 ' i ' >>> xset ([' H ',  ' J ',   ' e ',  ' t ']) >>> x.pop () ' h '

650) this.width=650; "src=" http://common.cnblogs.com/images/copycode.gif "alt=" Copy Code "/>


This article is from the "11043432" blog, please be sure to keep this source http://11053432.blog.51cto.com/11043432/1882434

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