Home > Article > Backend Development > Comparison of Python ancestors, lists, dictionaries, and sets
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.
##Definition | Method | |
List | 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 | |
Tuple | 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 | |
key-value pair, defined with {} 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, 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 | |
set() mutable collection | frozenset() immutable collection |
Methods (all collection methods): | For more articles about the comparison of Python ancestors, lists, dictionaries, and collections, please pay attention to the PHP Chinese website!