Python | Data type | Collection __python

Source: Internet
Author: User
Tags iterable
# encoding:utf-8 # set # unordered, not randomly accessible, not repeatable element set # Mixed operation of variable set and immutable set, result and match of operator on left side # definition: Variable Set print ({1, 2, 3}) print (Set ("Hellow Ord ")) print (Set ([1, 2, 3])) print (Set (1, 2, 3)) # Collection deduced print (SET (x * * * 2 for X in range (10))) # Definition of variable set S = Frozenset (
"Hello") s = Frozenset ([1, 2, 3]) s = Frozenset ((1, 2, 3)) # only the set S = Frozenset ({"Name": "Admin", "Age": 18}) that is generated by the key in the dictionary. Print (S, type (s)) # variable Set derivation s = Frozenset (x * * 2 for X in range (1) if x 2 = 0) print (S, type (s)) # When creating an empty collection, use Set ( or Frozenset () s = set () print (S, type (s)) s = Frozenset () print (S, type (s)) # collection element is a hash value, that is, immutable type # value, Boolean, string, tuple s = {1, 
True, "Hello", (1, 2, 3)} print (S, type (s)) # The elements in the collection cannot be duplicated, if repeated, merged into one L = List (set ([1, 2, 3, 1]) print (L, type (L)) # Common operations
# Add s = {1, 2, 3, 4} s.add (5) print (s) # delete # remove () # If there is no such element, the error s = {1, 2, 3, 4} s.remove (3) print (s) # discared () # If there is no such element, no error s = {1, 2, 3, 4} s.discard (3) print (s) # pop () # random deletion and return of an element in the collection # if no element deletion is the error s = {1, 2, 3, 4} result = S.pop () print (result, s)

# clear () # empty the set S = {1, 2, 3, 4} s.clear () print (s) # del s = {1, 2, 3, 4} del S # traverse # for in traversal s = {1, 2, 3, 4} F The or V in S:print (v) # iterator traverses s = {1, 2, 3, 4} its = iter (s) # print (next to) # Print (Next (its)) to V in Its:pri Operations between NT (v) # collections # intersection & s1 = {1, 2, 3, 4} s2 = {3, 4, 5, 6} result = S1.intersection (s2) print (Result, Typ E (Result)) # variable sets are intersected with immutable sets, with the type S1 = Frozenset ([1, 2, 3, 4]) s2 = {3, 4, 5, 6} intersection = S1.intersectio N (S2) print (results, type (result)) # Intersection_update to the original set # so it can only be used for variable sets S1 = {1, 2, 3, 4} s2 = {3, 4, 5, 6} s1.i  Ntersection_update (S2) print (S1, type (S1)) # intersection (iterable) print ({"1", "2"}.intersection ({"1": 89, "3": 90}) #
Union and |  S1 = {1, 2, 3, 4} s2 = {3, 4, 5, 6} result = S1.union (s2) print (results, type (result)) # The variable set is set with the immutable set, and the type of the result is the type of the Union left side S1 = Frozenset ([1, 2, 3, 4]) s2 = {3, 4, 5, 6} result = S1.union (s2) print (results, type (result)) # Update and result assignment update to original collection # so only Can be used for variable set S1 = {1, 2, 3, 4} s2 = {3, 4, 5, 6} s1.update (s2) print (S1, type (S1)) # Update (iterable) print ({"1", "2"}.union ({"1": 89, "3": 9 0})) # difference difference-S1 = {1, 2, 3, 4} s2 = {3, 4, 5, 6} result = S1.difference (s2) print (result, type (result)) # variable set and no The variable set is set and the type of the result is the type of the left of Union = Frozenset ([1, 2, 3, 4]) s2 = {3, 4, 5, 6} S1 = s1.difference (s2) print (Results, type (RES)
ult)) # Update and result assignment updated to original set # so it can only be used for variable sets S1 = {1, 2, 3, 4} s2 = {3, 4, 5, 6} s1.difference_update (s2) print (S1, type ()) # Update (iterable) print ({"1", "2"}.difference ({"1": 89, "3": 90})) # decision # Isdisjoint () # does not intersect S1 = {1, 2, 3, 4} s2 = {3 , 4, 5, 6} print (S1.isdisjoint (S2)) # Issuperset () # contains another collection S1 = {1, 2, 3, 4} s2 = {3, 4} print (S1.issuperset (S2)) # is Subset () # is included in another collection, print (S2.issubset (S1))

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.