Python (set, dict)

Source: Internet
Author: User

First, the collection

Its elements are unique and unordered.

1. Set definition

s = Set ()

s = {1, 2, 3}

2, the method of the collection

The set operation of the update version is modified on the original collection and the return value is none.

    • Add () represents the addition of an element to the collection. When an element that already exists is added, no exception is thrown. Hash can be used as a set of elements, not hash (List,set,bytearray dict) can not be a set of elements.
    • Update () indicates that an iterative object is added.
    • Remove () means that an existing element is removed from the collection, or a Keyerror exception is thrown.
    • Discard () indicates that an element is removed from the collection and does not throw an exception when the element does not exist.
    • A pop () indicates that an element is returned randomly from the collection and that the element is removed from the collection. Throws an Keyerror exception when an empty collection is deleted.
    • Clear () indicates that all elements are emptied.
    • Difference () represents all elements that are not in another collection, and returns a new collection.
    • Intersection () represents the intersection of two sets, returning a new collection.
    • Union () represents a set of two sets and returns a new collection.
    • Symmetric_difference () returns a new collection that returns an element that does not intersect two sets.
    • Difference_update () means that the original collection is modified to preserve only the elements that do not intersect the first collection.
    • Intersection_update () represents the same element in the original collection that retains two sets.
    • Issuperset () Indicates whether it is a superset.
    • Issubset () Indicates whether it is a subset.
    • Isdisjoint () represents two collective disjoint.

Second, the dictionary

A dictionary is a key-value pair structure. Its key must be a hash value and is unique, and value can be arbitrary.

1. Dictionary definitions

DT = Dict ()

DT = {}

2. Subscript operation

DT = {' A ': 2, ' B ': 3}

dt[' C ']=4

3. The method of the dictionary

    • Update () indicates that the added element will change the value of the original key if the same key is added. Its parameters can be in the following cases: dictionary, two-tuple-composed object, the key parameter.
DT = {' A ': 2, ' B ': 3}dt[' c '] = 4dt.update ([' G ', ' 3 '), (' H ', 2)]) dt.update ({' F ': 3, ' E ': 2}) dt.update (u=22) print (dt) #结果: {' A ': 2, ' B ': 3, ' C ': 4, ' G ': 3, ' H ': 2, ' F ': 3, ' E ': 2, ' U ': 22}
    • Pop () indicates that the specified key is removed from the dictionary and throws an Keyerror exception if the key does not exist, but does not throw an exception if the default value can be specified.
DT = {' A ': 2, ' B ': 3, ' C ': 4, ' G ': 3, ' H ': 2, ' F ': 3, ' E ': 2, ' u ': 22}d = Dt.pop (' gg ', -2) print (d) print (dt) #d结果: -2#dt result: {' A ': 2, ' B ': 3, ' C ': 4, ' G ': 3, ' H ': 2, ' F ': 3, ' E ': 2, ' U ': 22}
    • Popitem () indicates that a key is randomly removed from the dictionary and returns a tuple of key-value pairs. Throws an Keyerror exception if an empty dictionary is deleted.
DT = {' A ': 2, ' B ': 3, ' C ': 4, ' G ': 3, ' H ': 2, ' F ': 3, ' E ': 2, ' u ': 22}d = Dt.popitem () print (d) print (DT) #d结果: (' u ', ') #dt结果 : {' A ': 2, ' B ': 3, ' C ': 4, ' G ': 3, ' H ': 2, ' F ': 3, ' E ': 2}
    • Clear () Clears the dictionary of all key-value pairs.
    • Get () represents the value that accesses the specified key and can give the default value.
    • SetDefault () indicates that a default value can be specified for a key.

Third, the application of the dictionary

    • The dictionary may return all keys through the keys () method, or all values can be returned through the values () method.
    • Use the Len () method to count the length of the dictionary.

1, how to traverse a dictionary?

DT = {' A ': 1, ' B ': 2, ' C ': 3}for K in Dt.keys ():    
DT = {' A ': 1, ' B ': 2, ' C ': 3}for k,v in Dt.items ():    print (' {}=>{} '. Format (k,v), end= ')
#结果: a=>1 b=>2 c=>3

2. Enumerate usage

Enumerate () is a python built-in function and returns a enumerate object. For an iterative (iterable)/Ergodic object (such as a list, string), enumerate makes it an index sequence that can be used to obtain both the index and the value.

lt = [1, 2, 3, 4, 5, 6]for k,v in Enumerate (LT):    

Enumerate can also receive the second parameter, which specifies the index starting value.

lt = [1, 2, 3, 4, 5, 6]for k,v in Enumerate (lt,1):    

3, the use of ordereddict

There is a ordereddict in the collections module that remembers the order in which elements are inserted in the dictionary.

From collections Import Ordereddictot = Ordereddict () ot[' B ']=2ot[' a ']=1ot[' e ']=4ot[' 1 ']=1ot[' C ']=3ot[' 2 ']=2for k,v in Ot.items ():    print (' {}=>{} '. Format (k,v), end= ')
lt = {' A ', ' B ', ' C ', ' d '}print (Dict.fromkeys (lt,true)) #结果: {' A ': true, ' B ': true, ' d ': true, ' C ': true}

Python (set, dict)

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.