Handbook of python3.5 cultivation 6

Source: Internet
Author: User

General sequence Operations

All sequences in Python can perform certain operations. Includes index (indexing), shard (slicing), sequence addition (adding), multiplication (multiplying), membership, length, minimum, and maximum.

Index

The sequence is the most basic data structure in Python. Each element in the sequence is assigned a number that represents his position (index) in the sequence, the first index is 0, the second is 1, and so on.

All the elements in the sequence are numbered and increment from 0 onwards. You can access the elements of the sequence by number, respectively.

For example:

>>> greeting = ' Hello ' >>> greeting[0] ' H ' >>> greeting[1] ' e ' >>> greeting[2] ' l '

The elements in the sequence are numbered from left to right and the elements can be accessed by number, starting with 0.

How to get an element: add brackets to the variable, and enter the value of the element you are taking in parentheses.

Note that a string is a sequence that consists of characters. So the first element that 0 points to. For example, the above example, 0 points to H, and so on.


Gets the element from right to left in the opposite direction by number.

For example:

>>> greeting = ' Hello ' >>> greeting[-1] ' o ' >>> greeting[-2] ' l ' >>> greeting[-3] ' l ' >>> greeting[-4] ' e '

The rightmost element has a value of-1, which decrements from right to left, and never-0.

A left-to-right index in Pyhon is called a positive index, and right-to-left is called a negative index.

When you index a string, it is also possible to not define a variable.

For example:

>>> ' Hello ' [0] ' H ' >>> ' Hello ' [1] ' e ' >>> ' Hello ' [-1] ' O '

The effect of using an index directly is the same as defining a variable.


Use the function to return a sequence using interactive mode

>>> thirdth=input () [0]happy>>> thirdth ' h '

The result of the output can be seen, the return result of the function is indexed operation.

An index can either manipulate a reference to a variable, manipulate a sequence directly, or manipulate the return sequence of a function.



Sharding

Shards can be accessed within a range of elements, and shards are implemented by two indexes separated by colons.

For example:

>>> number=[1,2,3,4,5,6,7,8,9,10]>>> #取索引为第一个和第二个的元素 ... number[1:3][2, 3]>>> # Take the third and penultimate elements ... number[-3:-1][8, 9]

As can be seen from the operation results, the Shard operation supports both positive and negative indexes.

The implementation of a shard operation requires a two index as the boundary, the elements of the first index are covered in the Shard, and the elements of the second index are not covered in the Shard.

For example:

>>> number=[1,2,3,4,5,6,7,8,9,10]>>> #取最后三个元素 ... number[7:10][8, 9, 10]

Number is numbered 9, Number 10 points to the 11th element, is a nonexistent element, but after the last element, the last element can be obtained

For example:

>>> number=[1,2,3,4,5,6,7,8,9,10]>>> number[7:11][8, 9, 10]>>> number[7:12][8, 9, 10]

Small experiment: Think about why the output is empty.

>>> number=[1,2,3,4,5,6,7,8,9,10]>>> number[-3:0][]

Answer: As long as the leftmost index in the Shard appears in the sequence later than the right index, the result is an empty sequence

For example, in a small experiment, 3 represents the penultimate element, 0 means that the first element is the 3rd element that appears later than the first element, which is followed by the first element, so the result is an empty sequence.


What to do if you need to start from the end of the list and take the last value

For example:

>>> number=[1,2,3,4,5,6,7,8,9,10]>>> number[-3:][8, 9, 10]

You need to get the elements that include the end of the sequence, just set the second index to NULL as above


If using a positive index can achieve the above example effect

For example:

>>> number=[1,2,3,4,5,6,7,8,9,10]>>> #从第一元素开始输出, output all results ... number[0:][1, 2, 3, 4, 5, 6, 7, 8, 9, 10]>& Gt;> #最后一个元素为第一个, Output is empty >>> number[:0][]>>> #取得前3个元素 >>> number[:3][1, 2, 3] If the entire column is output, The two indexes are set to NULL for example:>>> number=[1,2,3,4,5,6,7,8,9,10]>>> number[:][1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

Step

Python also provides another parameter step (step length) that is usually implicitly designed, and in normal shards, the step size is 1. The Shard operation is to iterate through the elements of the sequence one step at a time, to return all elements between the start and end points, and to understand that the default step is 1, even if no step is set, the step implicitly sets the value to 1;

For example:

>>> number=[1,2,3,4,5,6,7,8,9,10]>>> number[0:10:1][1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

Set the step size to a number greater than 1

>>> number[0:10:2][1, 3, 5, 7, 9]

From the above example, we can see that the step setting is 2 to get odd sequence.

When the step size is set to greater than 1, you get a sequence that skips some elements

Flexible use of stride length

For example:

>>> number[0:10:3][1, 4, 7, 10]>>> number[2:6:3][3, 6]>>> number[2:5:3][3]>>> Number[1:5:3][2, 5]

You can also set a shortcut to empty the previous two indexes

For example:

>>> number[::3][1, 4, 7, 10]

Note: The step size cannot be 0

The step size can also be negative.

For example:

>>> number[10:0:-2][10, 8, 6, 4, 2]>>> number[0:10:-2][]>>> number[::-2][10, 8, 6, 4, 2]>&G T;> number[5::-2][6, 4, 2]>>> number[::-1][10, 9, 8, 7, 6, 5, 4, 3, 2, 1]>>> #第二个索引为0, not taking the 1th element in the sequence: . Number[10:0:-1]>>> #设置第二个索引为空, the first element of a sequence can be taken ... number[10::-1][10, 9, 8, 7, 6, 5, 4, 3, 2, 1]>>> #设置第二个索引为 Empty, can be taken to the first element of the sequence ... number[2::-1][3, 2, 1]>>> #第二个索引为0, not taking the 1th element in the sequence ... number[2:0:-1][3, 2]

Note: When using a negative step, set the second index to NULL to fetch the first element of the sequence.

The result of the previous example shows that the negative step is the opposite of the positive step.

Small sum of steps: For positive steps, Python extracts the right from the head of the sequence until the last element.

For negative steps, the element is extracted to the left from the end of the sequence until the first element.

Positive steps must make the start point smaller than the end point.

The negative step must make the start point larger than the end point.


sequence addition

Use the + sign to sequence the connection operation

For example:

>>> [3,4,5][1] + [2, 3, 3, 4, 5]>>> print (a+b) [1, 2, 8, 9]>>> c= ' du ' >>> d= ' Yu ' & Gt;>> e= ' Heng ' >>> print (c+d+e) Duyuheng

It is important to note that only sequences of the same type can be concatenated by the + sign, and different types of sequences cannot be concatenated by the + sign.


Multiplication

Multiply a sequence of rows with a number n multiplied by a sequence. In the new amount sequence, the original sequence repeats n times

For example:

>>> ' Duyuheng ' Duyuhengduyuhengduyuhengduyuhengduyuheng ' >>> [5]*5[5, 5, 5, 5, 5]

As you can see from the example above, the sequence is repeated the corresponding number of times, not the multiplication

Initializing a sequence of length x requires a null value for each encoding location, where a value is required to represent a null value, i.e. there are no elements inside

For example:

>>> #初始化5个为空的序列 ... sq=[none]*5>>> print (sq) [None, none, none, none, none]

None represents the built-in value of Python and represents nothing

Multiplication Small summary: sequence multiplication in Python can be done quickly with some initialization operations.


Member qualifications

The in operator is used to verify that a condition is true and to return the test result, which results in true return true, and the result is false return false.

For example:

>>> name= ' Duyuheng ' >>>  #检测字符串h是否早字符串中 ...  ' h '  in nameTrue>> >  #检测s是否在字符串内 ...  ' s '  in  namefalse>>> user=[' Duyuheng ', ' Duzhaoli ', ' Changting ']>>>  #检测字符串是否在字符串列表中 ...  ' changting '  in userTrue>>>  ' Xuwei '  in userFalse>>> numbers=[1,2,3,4,5]>>>  #检测数字是否在数字列表中 ...  1 in  numberstrue>>> 7 in numbersfalse>>> eng= ' & wo ai  ni * mei li de gu niang ' >>>  #检测特殊符号是否在字符串中 >>>  ' * '  in engTrue>>>  ' $ '  in engFalse>>>  ' a '  in  numbersfalse>>> 3 in nametraceback  (most recent call last):   File  "<stdin>", line 1, in <module>typeerror:  ' in < String> '  requires  String as left operand, not int>>> type (name) <class  ' str ' > 

A small summary of membership: use in to detect whether a character or number is in the corresponding list.

However, from the example above, it is shown that the numeric type cannot be checked for membership in the string type through in.

The string type can be checked for membership in the number type by using in


Length, minimum and maximum values

Python provides built-in functions for length, minimum, and maximum values, and the corresponding built-in functions are Len, Min, and Max respectively.

How to use

For example:

>>> numbenrs=[300,200,100,800,500]>>> len (Numbenrs) 5>>> max (NUMBENRS) 800>>> Min (NUMBENRS) 100

You can also use this

>>> Max (5,3,10,7) 10>>> min (5,3,10,-7)-7

Short summary of length, minimum and maximum values:

The Len function returns the number of elements contained in the sequence.

The Max function and the Min function return the maximum and minimum values in the sequence, respectively.

The parameters of the max and Min virtual functions are not a sequence, but rather the maximum and minimum values of multiple numbers are directly evaluated using multiple numbers directly as parameters.



This article from "Duyuheng" blog, declined reprint!

Handbook of python3.5 cultivation 6

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.