Six general procedure Python built sequences

Data structure (e.g. number of elements) in some way collection of data elements are grouped together, these data elements can be numbers or characters, or even other data structures. In Python, the basic structure is the data sequence (sequence). Each element in the sequence is assigned a serial number - i.e., the position of the element, also called an index. The first element is index 0, second is 1, and so once.

6 comprises a built-in Python sequence, i.e. a list of tuples, strings, Unicode string, buffer xrange objects and objects.

General Operation Sequence: index, slice, sequence summing, multiplication, membership, length, minimum and maximum

1. Index

Sequence all of the elements are numbered - from 0 increments. It can be accessed through the elements of a sequence number, respectively. Python sequences can also start from the right side of the index, the index of an element of the far right is -1, the left wants to start decreasing.

>>> greeting='Hello'  
>>> greeting[2]  
'l'  
>>> greeting[-1]  
'o'  
>>> 'stringtesting'[3] #可以对任何一个字符串进行索引,该字符串的第三个索引为'i'字符  
'i'  
>>> fourth=raw_input('Year: ')[3] #可以对输入的字符串进行索引,这样对输入的字符串索引为3的感兴趣  
Year: 2014  
>>> fourth  
'4'   

2. fragment

Index is used to access the individual elements, the elements can be accessed within a certain range by fragmentation, fragmentation is achieved by two spaced index colon. Slicing operation to achieve desirable to provide two indices as a boundary, the index of the first element is included in the slice, it is not included in the second slice.

'''
遇到问题没人解答?小编创建了一个Python学习交流QQ群:857662006 
寻找有志同道合的小伙伴,互帮互助,群里还有不错的视频学习教程和PDF电子书!
'''
>>> number=[1,2,3,4,5,6,7,8,9,10]  
>>> number[2:4] #取索引为第二和第三的元素  
[3, 4]  
>>> number[-4:-1] #负数表明是从右开始计数  
[7, 8, 9]  
>>> number[-4:] #把第二个索引置空,表明包括到序列结尾的元素  
[7, 8, 9, 10]  
>>> number[:3] #同上,把第一个索引置空,表明包含序列开始的元素  
[1, 2, 3]  
>>> number[0:10:1] #默认在分片的时候,步长为1,这样指定步长为1,和默认的效果一样  
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]  
>>> number[0:10:2] #这里指定步长为2,这样就会跳过某些序列元素  
[1, 3, 5, 7, 9]  
>>> number[10:0:-1] #步长也可以是负数,但是第一个索引一定要大于第二个索引  
[10, 9, 8, 7, 6, 5, 4, 3, 2]  
>>> number[10:0:-2]   
[10, 8, 6, 4, 2]  

For a positive step, Python will start sequence extracted from the head of the right elements, until the last element, and for a negative step, it is beginning to extract elements from the end of the list to the left until the first element

3 sequences are added

>>> [1,2,3]+[4,5,6]  
[1, 2, 3, 4, 5, 6]  
>>> 'Hello '+'World!'  
'Hello World!'  
>>> [1,2,3]+'Hello'  
Traceback (most recent call last):  
  File "<stdin>", line 1, in <module>  
TypeError: can only concatenate list (not "str") to list  

A final example, try to list and adding strings, but wrong, although they are all sequences, but different data types, can not be summed

4. Multiplication

With a sequence of digital x left to generate new sequence, the new sequence and the original sequence will be repeated x times.

'''
遇到问题没人解答?小编创建了一个Python学习交流QQ群:857662006 
寻找有志同道合的小伙伴,互帮互助,群里还有不错的视频学习教程和PDF电子书!
'''
>>> 'python'*4  
'pythonpythonpythonpython'  
>>> [None]*4 #None为Python的内建值,这里创建长度为4的元素空间,但是什么元素也不包含  
[None, None, None, None]  

5. Membership

May be used in operator to check whether a value in the sequence, in which if, Ture returned, if not, it returns False.

>>> permission='rw'  
>>> 'r' in permission  
True  
>>> 'x' in permission  
False  

6. length, minimum and maximum

Built-in function len, min and max return the number of elements contained in the sequence, and the smallest element in the sequence of the largest element.

>>> number=[2,3,4,5,6,7,8,9,10]  
>>> len(number)  
9  
>>> min(number)  
2  
>>> max(number)  
10  
>>> min(4,3,5) #函数的参数不用一定是序列,也可以是多个数字  
3  

Guess you like

Origin blog.51cto.com/14246112/2455024