Home  >  Article  >  Backend Development  >  Comparison of Python ancestors, lists, dictionaries, and sets

Comparison of Python ancestors, lists, dictionaries, and sets

高洛峰
高洛峰Original
2017-02-11 13:05:101296browse

The following editor will bring you a comparison of the ancestors of Python, lists, dictionaries, and sets. The editor thinks it is quite good, so I will share it with you now and give it as a reference for everyone. Let’s follow the editor and take a look.


can contain different types of objects, can add or subtract elements, and can be combined with other lists Combine or split a list, defined with [] eg: aList=[123,'abc',4.56,['inner','list'],7-9j]1.list (str): Convert str to list type, str can be a string or a tuple type 2. aList.append('test'): Append elements to the list 3. del aList[1]: Delete the list The element with subscript 1 del aList: delete the entire list 4.cmp(list1,list2): compare the sizes of the two lists 5.len(list): return the number of list elements 6.sorted(list): use lexicographic order Sort the elements in the list 7.reversed(list): reverse the position of the elements in the list 8.list.count(obj): return the number of times the object obj appears in the list 9.list.extend(seq): change the contents of the sequence seq Add to list 10.list.insert(index,obj): Insert the obj object at the index position 11.list.pop(index=-1): Delete and return the object at the specified position, the default is the last object 12.list.remove(obj): Remove the obj object from the list can contain objects of different types, but Immutable, elements cannot be added or subtracted, use () to define eg: aTuple=(123,'abc',4.56,['inner','list'],7-9j)1 .tuple(obj): Convert the object obj into a tuple object. obj can be any string or list 2. The del, cmp, len, max, and min methods applicable to lists are also applicable to tuples, but because the ancestor is immutable , replacement, addition, sorting, etc. are not possible##Dictionary1.dict1=dict((['x',1],['y',2])):dict()Create Dictionary 2.dict1={}.fromkeys(('x','y'),-1):fromkeys() creates a default dictionary, and the elements in the dictionary have the same value 3.dict1.keys(): Gets the dictionary Key value list 4.dict1.has_key('x'): Determine whether there is a 'x' key value in the dictionary, return bool type 5.dict.get(key,default): Return the value of the key value key, if the key does not exist , returns the value of default 6.dict.items(): returns the list of key-value pairs 7.dict.values(): returns the list of all values ​​in the dictionary 8.dict.update(dict2): returns the list of key-value pairs of dict2 Add to the dictionary dict 9.dict.pop(key): Returns the value of the key value key 10.setdefault(): Similar to the get method, it can obtain the value of the given key. In addition, setdefault can also automatically reset if it does not contain the given key. In the case of key, set the corresponding key-value 11.clear(): Clear all items in the dictionary. Operation in place, no return (or the return value is None) 12.copy(): Returns a new dictionary with the same key-value, which is a shallow copy Setset() mutable collectionFor more articles about the comparison of Python ancestors, lists, dictionaries, and collections, please pay attention to the PHP Chinese website!
##Definition Method
List
Tuple
key-value pair, defined with {} eg:aDict={'name ':'cynthia','age':18}

frozenset() immutable collection

Methods (all collection methods):
s.issubset(t) If s is a subset of t, return True, otherwise return False
s.issuperset(t) If s is a superset of t, return True, otherwise return False
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 that is a member of s but not a member of t, that is, returns the elements of s that are different from t
s.symmetric_defference(t) returns the set of all unique (not jointly owned) elements of s and t
s.copy() returns a shallow copy of s, which is more efficient than the factory

Methods (for mutable collections only): The following method parameters must be hashable
s.update(t): Modify s with elements in t, that is, s now contains members of s or t
s.intersection_update(t): The members in s are elements that belong to both s and t
s.difference_update(t): The members in s are elements that belong to s but are not included in t
s.symmetric_difference_update(t): The members in s are updated to those elements that are included in s or t but are not common to s and t
s.add(obj): Add object obj
to the collection s s.remove(obj): Delete the object obj from the set s. If obj is not an element in the set s (obj not in s), a keyError will be raised
s.discard(obj): If obj is an element in the set s, delete the object obj
from the set s s.pop(): Delete any object in the collection s and return it
s.clear(): delete all elements in the set s Comparing all the contents, I hope you will pay more attention to the PHP Chinese website~

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn