Python type set

Source: Internet
Author: User

Python's set is similar to other languages and is an unordered set of distinct elements, with basic functionality including relationship testing and de-duplication elements. The collection object also supports mathematical operations such as Union (union), intersection (intersection), Difference (poor), and sysmmetric difference (symmetric difference sets).

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

Example:

Print (' {} A word she can get what she {} for. ') Format (' With ', ' came ')) print (' {preposition} A word she can get to what she {verb} for. ') Format (preposition = ' with ', verb = ' came ')) print (' {0} A Worf she can get to what she {1} for. ') Format (' With ', ' came ')) S1 = set ([' LK ', ' Jim ', ' Tom ', ' Jim ') ' Print (S1) #打印会删除重复值s2 = s1.difference ([' Xzdz ', ' lk ']) print ( S2) #删除列表中的元素, the source file does not modify print (S1) #不变s1. difference_update ([' Xzdz ', ' lk ') print (S1) #源文件修改了s4 = S1.pop () print (S4) # The deleted element print (S1) #删除元素后x = set (' Szxpzm ') y = set ([' H ', ' Z ', ' m ', ' X ']) print (x&y) #交集print (x|y) #并集print (XY) #差集print (x , y)

Results:

With a word she can get how she came for. With a word she can get what she came for.with a Worf she can get to what she came for. {' Tom ', ' Jim ', ' LK '} {' Tom ', ' Jim '} {' Tom ', ' Jim ', ' LK '} {' Tom ', ' Jim '}tom{' Jim '} {' z ', ' x ', ' m '} {' x ', ' m ', ' Z ', ' s ', ' P ', ' h '} {' s ', ' P '} {' z ', ' x ', ' s ', ' m ', ' P '} {' z ', ' x ', ' m ', ' H '}

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