A comparison of Python ganso, lists, dictionaries, collections

Source: Internet
Author: User
Tags shallow copy

Defined Method
List Can contain different types of objects, you can add or subtract elements, can be combined with other lists or a list of split, using [] to define the eg:alist=[123, ' abc ', 4.56,[' inner ', ' list '],7-9j] 1.list (str): Convert str to list type, str can make string also can be tuple type 2.alist.append (' Test '): Append element to list 3.del alist[1]: Delete the element in the list labeled 1 del alist: Delete entire list 4.cmp (LIST1,LIST2): Compare the size of two lists 5.len (list): Returns the number of list elements 6.sorted (list): Sort elements in a list using a dictionary order 7.reversed (list): Position of element in inverted list 8.list.count (obj): Returns the number of times that the object obj appears in the list 9.list.extend (seq): Adds the contents of the sequence SEQ to the list 10.list.insert (index,obj): Inserts an Obj object 11.list.pop (index=-1) where the index is indexed: Deletes and returns the object at the specified position, default is the last object 12.list.remove (obj): Removes the Obj object from the list
Meta-group Can contain different types of objects, but is immutable, can not be added and subtracted elements, with () to define eg:atuple= (123, ' abc ', 4.56,[' inner ', ' list '],7-9j) 1.tuple (obj): Converts the object obj to a tuple object, and obj can be any string or List 2. The Del,cmp,len,max,min method for lists also applies to tuple, but because Ganso is immutable, substitution, addition, sorting, etc. are not possible
Dictionary Key-value pairs, using {} to define eg:adict={' name ': ' Cynthia ', ' Age ': 18}

1.dict1=dict ([' X ', 1],[' Y ', 2])):d ICT () to create a dictionary 2.dict1={}.fromkeys ((' x ', ' y '), -1): Fromkeys () creates a default dictionary in which the element has the same value 3. Dict1.keys (): Gets the dictionary's key value list 4.dict1.has_key (' x '): Determines if there is an ' X ' key value in the dictionary, returns the bool type 5.dict.get (Key,default): Returns the value of the key value key, if key does not exist, Returns the value of default 6.dict.items (): Returns the key-value pair list value 7.dict.values (): Returns a list of all values in the dictionary 8.dict.update (DICT2): Adds the DICT2 key-value pairs list to the dictionary Dict 9. Dict.pop (Key): Returns the value of key value 10.setdefault (): Similar to the Get method, the value of the given key can be obtained, In addition, SetDefault can also set the corresponding Key-value 11.clear () in case of automatic weight without a given key: Clears all entries in the dictionary. In-place operation, no return (or return value of None) 12.copy (): Returns a new dictionary with the same key-value as a shallow copy (shallow copy)

Collection

Set () Variable collection

Frozenset () Immutable collection

Method (All collection methods):
S.issubset (t) if S is a subset of T, returns True, otherwise false
S.issuperset (t) if S is a superset of T, returns True, otherwise false
S.union (t) returns a new collection that is the combination of S and T
S.intersection (t) returns a new collection that is the intersection of S and T
S.difference (t) returns a new collection that is a member of S, but not a member of T, that is, an element that returns s different from t
S.symmetric_defference (t) returns the collection of all S and T exclusive (non-co-owned) elements
S.copy () returns a shallow copy of S, which is more efficient than a factory

Method (for mutable collections only): The following method parameters must be hashed
S.update (t): modify S with elements in T, i.e. s now contains members of S or T
S.intersection_update (t): Members in S are elements that collectively belong to S and T
S.difference_update (t): Members in S are elements that belong to s but are not contained in T
S.symmetric_difference_update (t): Members in S are updated to those elements that are contained in s or T, but are not common to s and T
S.add (obj): Adding an object in collection s obj
S.remove (obj): Object obj removed from collection s, if obj is not an element in collection s (obj not in s), a keyerror error is thrown
S.discard (obj): If obj is an element in the collection s, delete the object from the collection s obj
S.pop (): Deletes any object in the collection S and returns it
S.clear (): Delete all elements in the collection s

A comparison of Python ganso, lists, dictionaries, 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.