Python first day-Getting Started Python (5) Collection operations

Source: Internet
Author: User

This article focuses on some simple operations for collections

Sets are divided into mutable sets and immutable sets

Variable set (non-hash)

S={1,'a', (+/-),}  # equals set ({1, ' a ', (+/-),})

Add Value

S={1,'a', (All-you-do),}print(s) s.add ('123 ' )print(s)

Clear Collection

S={1,'a', (+/-),}print(s) s.clear ()  Print(s)

Copy Collection

S={1,'a', (A/b),}print(s) a=s.copy ()Print (a)

Random Delete

S={1,'a', (+/-),}print(s) s.pop ()Print (s)

Specify delete element

S={1,'a', (A/b),}print(s) s.remove (1)Print (s)

Non-repeatable properties

Immutable collections

Less use

Calling the Frozenset method

F=frozenset ({1,2,3,4})

When you're done, you'll find no F.add,f.pop,f.remove method.

Variable Collection Common operations

Example:
python_list=['Tom','Jack','Lucy']openstact_list=['Rose','Tom','xiaoming','Lucy']p_u=Set (python_list) O_u=set (Openstact_list)#define a list as a collection

Subtraction

# learn only OpenStack courses without learning python lessons, and vice versa Res=o_u- p_uprint(res)

and set:

Res=p_u | O_u Print (RES)

Intersection:

Res=p_u & O_uprint(res)

Complementary set:

Res=p_u ^ O_uprint(res)

Parameter operation:

Print (P_u.difference (O_u))  # Straight Results p_u.difference_update (O_u)# Find the results and update print(P_u)

P_u.discard ('Tom')  # If the collection is removed, nothing is done print(P_u) P_u.discard ('asdasdasd')print(P_u)

Print (P_u.intersection (O_u))      # intersection # Output and update
Res=p_u.union (O_u)  # and set print(res)
# Cross complement set Print (RES)

Python first day-Getting Started Python (5) Collection operations

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.