Comparison of Python ancestor, list, Dictionary, set, and python

Source: Internet
Author: User
Tags new set

Comparison of Python ancestor, list, Dictionary, set, and python

Definition Method
List You can include different types of objects, add or remove elements, and combine them with other lists or split a list. For example: aList = [123, 'abc' defined ', 4.56, ['inner ', 'LIST'], 7-9j] 1. list (str): converts str to the list type. str can be a string or a tuples. aList. append ('test'): append the element to the list to go to 3.del aList [1]: Delete the element marked as 1 in the list del aList: Delete the entire list 4.cmp( list1, list2 ): compare the size of the two lists 5.len( list): return the number of list elements 6. sorted (list): sort the elements in the list by lexicographically 7. reversed (list): place the elements in the inverted list 8. list. count (obj): returns the number of times the object obj appears in the list. 9. list. extend (seq): add the content of sequence seq to list 10. list. insert (index, obj): insert obj object 11 where the index volume is index. list. pop (index =-1): delete and return the object at the specified position. The default value is the last object 12. list. remove (obj): Delete the obj object from the list.
Tuples It can contain different types of objects, but it is immutable. You cannot add or remove elements. You can use () to define eg: aTuple = (123, 'abc', 4.56, ['inner ', 'LIST'], 7-9j) 1. tuple (obj): Convert object obj to tuple object. obj can be any string or list 2. the del, cmp, len, max, and min methods for the list are also applicable to tuple. However, because the ancestor is immutable, replacement, addition, and sorting cannot be implemented.
Dictionary Key-value pairs. Use {} to define eg: aDict = {'name': 'cynthia ', 'age': 18}

1. dict1 = dict (['x', 1], ['y', 2]): dict () Create dictionary 2. dict1 = {}. fromkeys ('x', 'y'),-1): fromkeys () creates a default dictionary. The elements in the dictionary have the same value. 3. dict1.keys (): Get the dictionary key value list 4. dict1.has _ key ('x'): determines whether the dictionary has a 'X' key value. The return value belongs to the bool type 5. dict. get (key, default): return the value of the key value. If the key does not exist, the default value 6 is returned. dict. items (): Return key-value pairs List Value 7. dict. values (): returns the list of all values in the dictionary. dict. update (dict2): add the list of key-value pairs of dict2 to the dictionary dict. dict. pop (key): return the value 10 of the key. setdefault (): similar to the get method, the value of the given key can be obtained. In addition Etdefault can also set the corresponding key-value 11. clear () when auto-duplicate does not include a given key: clear all the items in the dictionary. In-situ operation, no return (or the return value is None) 12. copy (): return a new dictionary with the same key-value, shallow copy)

Set

Set () variable set

Frozenset () unchangeable set

Method (all set methods ):
S. issubset (t) If s is a subset of t, True is returned; otherwise, False is returned.
S. issuperset (t) If s is a superset of t, True is returned; otherwise, False is returned.
S. union (t) returns a new set, which is the union of s and t.
S. intersection (t) returns a new set, which is the intersection of s and t.
S. difference (t) returns a new set, which is a member of s, but not a member of t, that is, returns elements of s different from t.
S. symmetric_defference (t) returns a set of unique (not co-owned) elements of s and t.
S. copy () returns a small copy of s, which is more efficient than the factory

Method (only applicable to variable sets): the following method parameters must be hashable
S. update (t): use the element in t to modify s, that is, s now contains s or t members.
S. intersection_update (t): Members in s are elements of s and t.
S. difference_update (t): the members in s belong to s but are not included in t.
S. symmetric_difference_update (t): Members in s are updated to elements that are included in s or t but not shared by s and t.
S. add (obj): add object obj to set s
S. remove (obj): delete object obj from set s. If obj is not an element (obj not in s) in set s, A keyError error is thrown.
S. discard (obj): If obj is an element in set s, delete the object obj from set s.
S. pop (): delete any object in set s and return it.
S. clear (): deletes all elements in set s.

The above is a little bit about the Python ancestor, list, dictionary, and collection. I hope you can support more help ~

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.