Summary of common methods for Python collections

Source: Internet
Author: User

Data type: Int/str/bool/list/dict/tuple/float/set (set type natural deduplication)

I. Definition of a collection

s = set () #定义空集合

s = {' A ', ' B ', ' C ', ' d '} #集合不是key-value-shaped, no colon

The set is unordered and cannot be evaluated by subscript.

Second, set assignment value

S.add ()

s = {' A ', ' B ', ' C ', ' d '}
S.add (' Ijk ') difference between #注意add and update
# s.update (' FGH ')
Print (s)

Output Result:

{' d ', ' Ijk ', ' C ', ' B ', ' A '}

S.update ()

Output Result:

{' F ', ' B ', ' G ', ' d ', ' a ', ' C ', ' H '}

s = Set ()

s = set (' Cheeseshop ')
Print (s)

Output Result:

{' s ', ' e ', ' P ', ' h ', ' O ', ' C '}

Third, delete the collection element

S.remove ()
s = set (' Cheeseshop ')
S.remove (' er ') # Delete non-existent will error
S.remove (' e ')
Print (s)

s.pop () #随机删除一个

s.discard (' er ')  #如果删除的元素存在, delete, do not exist do not handle

del s # Delete Collection

Iv. Collection of common operations

S-= set (' Copy ') equivalent to S = S-set (' copy ')

Take intersection

S.intersection (S1) equivalent to S & S1

Fetch and set

S.union (S1) equivalent to S | S1

Take the difference set

S.difference (S1) is   equivalent to  s-s1


Summary of common methods for Python collections

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.