Python list tuple dictionary collection summary

1. Variable name

Variable names are like the names of our real society. When a value is assigned to a name, it will be stored in memory and called a variable. In most languages, this behavior is called "assigning a value to a variable" or "Store the value in a variable". However, in Python, unlike most other programming languages, Python does not store values ​​in variables, but rather sticks names on top of the values. It can be considered that Python has no variables, only names. Python does not need to declare variable names, just summarize the assignment!

x=1
y=1.1111
#都是直接赋值不声明变量

2. List

List is one of the important built-in variable sequences in Python , and it is an ordered continuous memory space containing several elements . In the form of all the elements in one pair of brackets [] in, so that between adjacent elements separated by a comma , when adding or deleting a list of elements, the list of objects automatic memory expanded or contracted , so as to ensure between the elements There is no gap, this automatic memory management function of Python lists can greatly reduce the burden on programmers, but inserting and deleting non-tail elements will involve the movement of a large number of elements in the list, which is extremely inefficient, and may cause unexpected results for certain operations. Wrong result, so unless really necessary, you should try to add or delete elements from the end of the list . Commonly used list object method function or method description ls[i] = x replace the i-th data item of the list ls with x ls[i: j] = lt replace the i-th to j item data in the list ls with the list lt (excluding the jth item) Item, the same below) ls[i: j: k] = lt replace the data from the i to j in the list ls with k as the step with the list lt del ls[i: j] delete the data from the i to j item of the list ls, etc. Value at ls[i: j]=[] del ls[i: j: k] delete the data from the i to j of the list ls with k as the step ls += lt or ls.extend(lt) add the element of the list lt to In the list ls, ls *= n updates the list ls, and its elements are repeated n times. ls.append(x) adds an element x at the end of the list ls. ls.clear() deletes all the elements in ls. ls.copy() generates a new list, Copy all the elements in ls. ls.insert(i, x) Add an element x at the i-th position of the list ls.pop(i) Take out the i-th element from the list ls and delete the element ls.remove(x) Put it in the list The first element x that appears is deleted ls.reverse(x) The elements in the list ls are reversed

3. List comprehension

List comprehensions can use a very concise way to quickly generate lists that meet specific needs. The code is very readable. In addition, Python's internal implementation has made a lot of optimizations on list comprehensions to ensure fast running speed. The grammatical form of the list comprehension is: [expression for variable in sequence or iteration object]

4. Slice

Slicing, in form, slicing is completed by using 3 numbers separated by 2 colons. The first number indicates the starting position of the slice (default is 0), and the second number indicates the cutoff (but not including) position of the slice (default Is the length of the list), the third number indicates the step length of the slice (default is 1), when the step length is omitted, the last colon can be omitted at the same time.

5. Slicing effect

Slicing is suitable for lists, tuples, strings, range objects and other types, and has the most powerful function when applied to lists. You can use slicing to intercept any part of the list and return to a new list. You can also use slicing to modify and delete some elements in the list. You can even add elements to the list object through slicing.

6, tuple

Tuples are also an important sequence structure in Python. Formally, all elements of a tuple are placed in a pair of parentheses, and the elements are separated by commas. A tuple is an immutable sequence. Once created, there is no way to modify the value of the element in the tuple, nor can it add or delete elements to the tuple. Therefore, the tuple does not provide methods such as append(), extend() and insert(). It is impossible to add elements to the tuple. There is also no remove() and pop() methods in the same element, nor does it support delting on tuple elements. Operation, you cannot delete elements from the tuple, but can only use the del command to delete the entire tuple. Tuples also support slicing operations, but the elements in the tuple can only be accessed through slicing.

7. Generator deduction

The generator comprehension is very close to the list comprehension in form, except that the generator comprehension uses parentheses instead of the square brackets used in the list comprehension. Different from the list comprehension, the result of the generator comprehension is a generator object, not a list or a tuple. When using the elements of the generator object, you can convert it into a list or a tuple as needed. You can use the __next__() method of the generator object or the built-in function next() to traverse, or directly use it as an iterator object. But no matter which method is used to access its elements, after all element access is over, if you need to revisit the elements in it, you must recreate the generator object.

8. Dictionaries

A dictionary is an unordered variable sequence containing several "key: value" elements. Each element in the dictionary contains two parts, "key" and "value", which represent a mapping or correspondence. When defining a dictionary, the key and value of each element are separated by colons, and different elements are separated by commas. All elements are placed in a pair of braces "{" and "}". The "key" in the dictionary can be any immutable data in Python, such as integers, real numbers, complex numbers, strings, tuples, etc., but you cannot use lists, sets, dictionaries or other variable types as the "keys" of the dictionary. In addition, the "key" in the dictionary is not allowed to be repeated, while the "value" can be repeated.

9. Collection

The collection can only contain immutable types of data such as numbers, strings, and tuples, but cannot contain variable types of data such as lists, dictionaries, and collections . Python provides a built-in function hash() to calculate the hash value of an object. Any object that cannot calculate the hash value (throwing an exception when calling the hash() function) cannot be used as an element of a collection, nor can it be used as a dictionary object "key". The built-in functions len(), max(), min(), sum(), sorted() and the membership test operator in are also applicable to sets.

Guess you like

Origin blog.csdn.net/qq_45396672/article/details/103080249