Sequence common operations of notes in the basic python tutorial, and python Sequence
All the elements in the sequence are numbered-starting from 0. When a negative index is used, Python starts counting from the right, that is, from the last element. The position number of the last element is-1. in addition, a string is a string consisting of characters. You can directly use an index for the string literal value. If a function call returns a sequence, you can index the returned result directly. For example
# Print the date months = ['january ', 'february', 'march', 'may', 'june', 'August ', 'September ', 'October', 'November ', 'december'] # list endings ending with numbers 1-31 = ['st', 'nd ', 'RD '] + 17 * ['th'] + ['st', 'nd ', 'RD '] + 7 * ['th'] + ['st'] year = raw_input ('year:') month = raw_input ('month (1-12 ): ') day = raw_input ('day (1-31):') month_number = int (month) day_number = int (Day) # Remember to reduce the number of months and days by 1, to get the correct index month_number = months [month_number-1] ordinal = day + endings [day_number-1] print month_number + ''+ ordinal + ',' + year
Result
Annual: 1985
Month (1-12): 2
Day (1-31): 5
February 5th, 1985
The sharding operation can access elements within a certain range. The sharding is implemented by two indexes separated by colons. The first index is the number of the first element to be extracted, the last index is the number of the first element in the remaining part after the shard, as shown in figure
>>> Numbers = [1, 2, 4, 5, 6, 7, 8, 9]
>>> Numbers [3: 6]
[4, 5, 6]
If the part obtained contains the element at the end of the sequence, you only need to leave the last index empty. This method is also applicable to the element at the beginning of the sequence, such
>>> Numbers = [1, 2, 4, 5, 6, 7, 8, 9]
>>> Numbers [7:]
[8, 9]
>>> Numbers [: 3]
[1, 2, 3]
In common parts, the step size is implicitly set to 1. For a positive step, Python extracts elements from the right of the sequence header until the last element. For a negative step, it extracts elements from the end of the sequence to the left, until the first element, such
>>> Numbers = [1, 2, 4, 5, 6, 7, 8, 9]
>>> Numbers [0: 3]
[1, 2, 3]
>>> Numbers [3: 0:-1]
[4, 3, 2]
The plus sign can be used to connect the sequence. The sequence type must be the same, as shown in figure
>>> [4, 3, 2] + [8, 9]
[4, 3, 2, 8, 9]
>>> 'Hello' + 'World'
'Hello world'
>>> [8, 9] + 'World'
Traceback (most recent call last ):
File "<pyshell #13>", line 1, in <module>
[8, 9] + 'World'
TypeError: can only concatenate list (not "str") to list
Multiply the number x by a sequence to generate a new sequence. In the new sequence, the original sequence will be repeated x times, as shown in figure
>>> 5 * 'hl'
'Hlhlhlhlhlhl'
[] -- Empty list, with nothing in it
None -- no elements are placed in it
>>> Seq = 10 * [None]
>>> Seq
[None, None]
# Print a sentence Sentence = raw_input ("sentence:") screen_width = 80text_width = len (sentence) in the centered "box" with the correct width) box_width = text_width + 6left_margin = (screen_width-box_width) // 2 printprint ''' * left_margin + '-' * (box_width-4) + '+ 'print ''' * left_margin +' | '+ ''' * text_width +' | 'print ''' * left_margin + '|' + sentence + '| 'print'' * left_margin + '|' + ''' * text_width + '| 'print ''' * left_margin +'-'* (box_width-4) + 'print
Result
Sentence: He's a very naughty boy!
+ -------------------------- +
|
| He's a very naughty boy! |
|
+ -------------------------- +
You can use the in operator to check whether a value is in a sequence. If yes, True is returned. If no value exists, False is returned. For example:
>>> Numbers = [1, 2, 4, 5, 6, 7, 8, 9]
>>> 8 in numbers
True
>>> 34 in numbers
False
- Length, minimum, and maximum
Len -- returns the number of elements in the sequence.
Min -- returns the smallest element in the sequence.
Max -- returns the largest element in the sequence.
For example
>>> Numbers = [1, 2, 4, 5, 6, 7, 8, 9]
>>> Len (numbers)
9
>>> Min (numbers)
1
>>> Max (numbers)
9