This article provides an in-depth analysis of python dict, set, list, tuple applications and corresponding examples, which will help readers to grasp the concepts and principles. The details are as follows:
1. Dictionary (dict)
dict is surrounded by {}
(),(),()
hash(obj) returns the hash value of obj, if returned means it can be used as the key of dict
del or can delete an item, clear clears all the contents.
sorted(dict) sorts the dict.
() can look for keys that don't exist, dict.
() Checks if the dictionary contains a key. If the key exists in the dictionary, you can fetch its value. If the key you're looking for doesn't exist in the dictionary, you can assign a default value to the key and return that value.
{}.fromkeys() creates a dict, e.g..
{}.fromkeys(('love', 'honor'), True) =>{'love': True, 'honor': True}
Do not allow a key to correspond to multiple values
Keys must be hashed, test with hash()
An object that implements the _hash()_ method can be used as a key value
2. Sets
A set is a mathematical concept created with set()
(),, add update delete, -= can do set subtraction
is different in that if the deleted element is not in the set, discover does not report an error, but remove does.
< <= denotes subset, > >= denotes superset
| denotes union & denotes intersection - denotes difference set ^ difference set
3. List (list)
Lists are sequence objects that can contain any Python data information, such as strings, numbers, lists, tuples, and so on. The data in a list is mutable, and we can add, modify, delete, etc. the data in the list through object methods. A sequence type can be converted into a list by using the list(seq) function.
append(x) appends a single object x to the end of the list. using multiple arguments raises an exception.
count(x) returns the number of times object x appears in the list.
extend(L) Adds a table entry from list L to the list. Returns None.
Index(x) Returns the index of the first list item in the list that matches object x. An exception is thrown if there are no matching elements.
insert(i,x) Inserts object x before the element at index i. e.g. (0,x) inserts the object before the first item. Returns None.
pop(x) removes the table entry in the list with index x and returns the value of that table entry. If no index is specified, pop returns the last item in the list.
remove(x) Removes the first element of the list that matches object x. An exception is thrown when an element is matched. Returns None.
reverse() reverses the order of the list elements.
sort() Sorts the list, returning none. bisect module can be used to sort the addition and removal of list items.
4. Tuple (tuple)
tuple=(1,), which is a tuple representation of a single element, with an extra comma.
tuple=1, 2, 3, 4, which can also be a tuple, Python allows tuples without parentheses when they do not lead to confusion.
Like lists, tuples can be indexed, sliced, concatenated and repeated. You can also use len() to find the length of a tuple.
The index of the tuple is in the form tuple[i], not tuple(i).
Similar to lists, other sequence types can be converted to tuples using tuple(seq).