Python collection type __python

Source: Internet
Author: User
There are two types of set:variable set and immutable set:variable set can not be hashed. Note:frozenset can not be changed!

1.create and update

> Myset=set ("abadsfljasd")
> MySet
set ([' A ', ' B ', ' d ', ' F ', ' j ', ' l ', ' s '])

Note:we can not create a set by operator (can not treated as list ant tuple or diretoty). We can only use Set () ANF Frozenze T ().
1.1visit The value in a set:

' A ' in MySet
1.2update a set

Myset.add (' z ')
myset.update ("python")
myset.remove ("ABCD")
s-=set ("Python")

1.3delete a setDel MySet

2.the Oprator of Set 2.1 Standard operatorIn,no in,==,!=, >, >=, <=

2.2 ' Set ' type operator

Combine: "|"
> Twoset=se ("xyz")
> Oneset | twoset
Set ([' A ', ' C ', ' B ', ' d ', ' Y ', ' x ', ' Z '])
And,-, ^
>oneset ^ threeset
>set ([' C ', ' d ', ' 1 ', ' 3 ', ' 2 '])
Mixed-type Operate:set and frozen set
> Myset=set ("ABCD")
> Myfrozenset=frozenset ("1234")
> MySet |  Myfrozenset
Set ([' A ', ' C ', ' B ', ' d ', ' 1 ', ' 3 ', ' 2 ', ' 4 '])
> Myfrozenset | myset
frozenset ([' A ', ' C ', ' B '), ' d ', ' 1 ', ' 3 ', ' 2 ', ' 4 ']
Note:the result type follow the former.

2.3set-type operator---only used for set is not for Frozensetupdate:|=, &=, =, ^=

3.build_in functionLen (), set (), Frozenset ()

3.1 set-type build_in functionnote:build_in function means Standart build_in function that could the for set. Set-type build_in function means this function are part of Set-type.

Function name return value

S.add (' a ') update s
S.clear () Clear S
S.difference (t) a new set (element in S but not in T)
S.union (t) a new set
S.intersection (t) a new set
S.issubset (t) True or False

3.2compare:operator and build_in function: For the most, build_in function and Operarot is equal. However, the operand for the operator must is Set-type, while the object for the Build_in function could is iterative type .

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.