Python data type, python

Source: Internet
Author: User

Python data type, python

1. Note the difference between list and set. set

List representation: list_1 = [1, 3, 4]; set representation: set_1 = set ()

List_1 = [1, 2, 3, 4, 23,4, 2] print (list_1, type (list_1) list_1 = set (list_1) print (list_1, type (list_1 )) list_2 = set ([2, 4, 6, 8, 10]) print (list_2, type (list_2) # running result [1, 2, 3, 4, 23, 4, 2] <class 'LIST'> {1, 2, 3, 4, 23} <class 'set'> {8, 2, 10, 4, 6} <class 'set'>

2. Set relationship:

########################### Link test part of a set ###### ############################ intersection print (list_1.intersection (list_2 )) # print (list_1.symmetric_difference (list_2) of the intersection of two sets except for the symmetric difference set # print (list_1.union (list_2) of the Union set # difference set # is in list_1, but not in list_2print (list_1.difference (list_2) # is in list_2, but not in list_1print (list_2.difference (list_1) # subset list_3 = set ([6, 8, 10]) print (list_3.issubset (list_2) # print (list_2.issuperset (list_3) # "" Return True if two sets have a null intersection. "print (list_1.isdisjoint (list_3) print (list_1.isdisjoint (list_2 ))'''

"& |-^" Another way to express the set relationship

# Intersection print ("intersection->", list_1 & list_2) # unionprint ("union->", list_1 | list_2) # differenceprint ("difference -->", list_1-list_2) # is in list_1 but not in list_2 # symmetric difference set print ("symmetric difference set -->", list_1 ^ list_2)

3. add, update, remove, len, in, not in, pop, discard

List_1 = (,) list_2 = ([,]) list_3 = set ([,]) print (list_1, type (list_1) print (list_2, type (list_2) print (list_3, type (list_3) #1. add an add item and add multiple updatelist_3.add (9) print ("test1 --", list_3) list_3.update ([11,13]) print ("test2 --", list_3) #2. remove a list_3.remove (11) print ("test3 --", list_3 )#. 3 length print ("test4 --", len (list_3) #4. print ("test5 ---", 6 in list_3, 3 in list_3, 11 not in list_3) #5. delete any set element and return print (list_3.pop () list_3.discard () # Remove an element from a set if it is a member. if the element is not a member, do nothing. list_3.remove () # Remove an element from a set; it must be a member. if the element is not a member, raise a KeyError

 

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.