2013年8月29日星期四

Python sequence (Sequence)

 

Sequence is a Python built-in types (built-in type), built-in type that is built on Python Interpreter inside type, three basic The Sequence Type is list (table), tuple (setting the table, or translated into tuple), range (range) . Can be seen as Python Interpreter defines such three class.

 

1, list, table

 

Python has a range of complex data types, is one of the most versatile list

 

class list ([iterable])

 

list with enclosed in square brackets and separated by commas , membership is not the same type can be, but is generally a type.

 

1.1 list object construction

 
  
>>> list = [] 
>>> list = [1,3]
>>> list
[
1, 3]
 
 You can also build this

 
  
>>> list()   # empty list 
[]
>>> [x for x in range(11)] # [x for x in iterable]
[0,
1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
>>> list((3,5,6)) # list(iterable)
[
3, 5, 6]
 
 

 

1.2, change the list element value

 

list and is not the same string, string is immutable (immutable) type, list is Variable (mutable), on the list can be changed in whole or in part

 
  
>>> list = [list, 5] 
>>> list
[[
1, 3], 5]
>>> list[1] = 6
>>> list
[[
1, 3], 6]
 
 

list can be "slice" slice , to get a sub-list, it can "slice" assignment, change list

 

range reference syntax is [Lower: Upper: step], default step size is 1 , past few subscript from 0 start, from behind the subscript from -1 Start

 
  
>>> list = [1,2,3,4,5] 
>>> list[2:4]
[
3, 4]
>>> list[-3:]
[
3, 4, 5]
 
 The sections were assigned

change list

 
  
>>> list 
[
1, 2, 3, 4, 5]
>>> list[3:] = [5,4]
>>> list
[
1, 2, 3, 5, 4]
 
 

can one see from the above, change the list contents for operations such as assignment, do not print the results, which is the expression returns none. This is Python variable data structures (mutable Data Structure) design principles.

 

 

2, tuple

 

tuple is an immutable of (immutable)

 

class tuple ([iterable])

 

tuple construction

 
  
seq = ()  或  seq = tuple()   #构建empty tuple 
seq = (4,) 或 4, #构建只有一个元素的tuple (4),逗号是必不可少的,不然会返回数字4
seq = (3,4,5) 或 seq = 3,4,5
seq = tuple([3,4,5]) # 生成 (3,4,5)
 
 

iterable can be any support iteration of Sequence, Container. We use the parameters above list [3,4,5], also is to use tuple (3,4,5)

 

For tuple, it is important that the comma "," instead of parentheses, brackets from the above we can see that there is no ambiguity can be omitted, but some places have to be, for example,

 

f (a, b, c) indicates that the function takes three arguments

 

and f ((a, b, c)) indicates that the function accepts one parameter, which is a ternary tuple.

 

 

tuple can not be assigned, so the tuple used to do a different type ( heterogeneity ) set of sequences of elements, and list it used to do the same type ( homogeneity ) collection of elements.

 

 

3, range

 

range is also a type (type), which is a of digital serial (s sequence of numbers) , but immutable , and are often used in a for loop in

 

class range (stop)

 

class range (start, stop [, step])

 

For the first construction method, start the default value is 0, step defaults 1

 

When the step is positive, a range of element value r [i] = start + i * step and r [i] << / span> stop; step is negative, r [i] > stop

 
  
>>> range(6) 
[0, 1, 2, 3, 4, 5]
>>> tuple(range(0,-10,-2))
(0, -2, -4, -6, -8)
 
 

 

4. multiple assignment ( multiple assignment )

 

multiple assignment is such an assignment expression.

 
  
1 t = a,b,c 
2 a,b,c = t
 
 

and multiple assignment essence tuple packing and Sequence unpacking .

 

>>> t = a, b, c # which is tuple packing, constructed according to the syntax of a tuple, we know where t must be a tuple.

 

>>> a, b, c = t # This is Sequence unpacking, where t is the ternary Sequence long as can be, not necessarily the tuple, if t is not ternary, will throw a ValueError exception.

 

 

-----------------

 

Reference:

 

http://www.cnblogs.com/vamei/ archive/2012/05/28/2522677.html Vamei's blog

 

http:// docs.python.org/3/library/stdtypes.html # sequence-types-list-tuple-range Python documentation, built-in type Sequence

 

http://docs.python.org/ 3/tutorial/datastructures.html # tuples-and-sequences Python documentation, tutorial

没有评论:

发表评论