The second chapter summarizes the data structure

The most common classification Python sequence types is variable and immutable sequence . However, another classification is also useful, that is, put them into a flat container and sequence sequences . Volume of the former is smaller, faster and simpler to use, but it can only hold a number of atomic data, such as numbers, characters and bytes. Container sequence is more flexible, but when the vessel encountered variable sequence objects, users need to be careful, because this combination often come up with some "accident", especially with nested data structure appears, the user should Some thought a little extra to ensure that the correct code.

List comprehensions and generator expressions provides a flexible way to build and initialization sequences, these tools are extremely powerful. If you can not use them skillfully, you can devote time to practice. They is not difficult, but to use them addictive.

In Python tuple plays two roles, both can be used as a record field without a name, but also can be seen as an immutable list. When the tuple is recorded as to use when unpacking it is the most secure and reliable way to extract information from different fields tuple. * The introduction of new tuple unpacking syntax allows a higher level of convenience, allowing users to selectively ignore unneeded fields. Named tuples also not a new concept, but it does not seem to receive the attention they deserve. Like ordinary tuples, named instances tuple is also very space-saving, but it also provides a convenient place to get information on the various fields of the tuple name by the way, in addition to a practical ._asdict () method to put the record become OrderedDict type.

The most popular Python language feature is a sequence of slices, and many people are actually not fully understand its powerful place. For example, a user-defined type can also be selected sequence Duo Weiqie support sheet and omissions in NumPy (...). In addition, the slice assignment is a variable sequence modified shortcut.

Repeat stitching seq * n in the proper use of the premise, allowing us to easily initialize a multidimensional list containing the immutable elements. Incremental assignment and + = * = variable would be treated differently and immutable sequence. In the face of immutable sequence, these two operations will generate a new sequence in the back. But if the object is assigned is variable, then the sequence will modify the place - but it also depends on

Sequence itself implement special methods.

sort sequence-sorted and built-in functions, although very flexible, but using them is not difficult. These two methods are more flexible because they accept a function as an optional parameter to specify how the sorting algorithm to compare the size, this parameter is the key parameter. key may also be used in the min and max functions. If you want to insert a new element at the same time maintaining order in an ordered sequence, then the need to use bisect.insort. The effect is to quickly find bisect.bisect.

In addition to lists and tuples, Python standard library also array.array. In addition, although NumPy and SciPy are not part of the Python standard library, but learning a little about them, and you will divine intervention when dealing with large-scale numerical data.

The end of the chapter describes the collections.deque this type, it has a characteristic versatile and thread-safe. Table 2-3 lists the API and it was compared. Finally, this chapter also mentioned some other type of queue standard library implementation.

Guess you like

Origin www.cnblogs.com/yuchen991/p/12441562.html
Recommended