Data analysis using python Second Edition (Python data structures, functions and files) and the data structure sequence [1-1]

1. tuple
tuple is a fixed length, immutable Python sequence object. The easiest way to create a tuple, is
separated by a comma value:


When a complex expression defines tuple values ​​are best placed in parentheses, are as follows:

1 In [3]: nested_tup = (4, 5, 6), (7, 8)
2 In [4]: nested_tup
3 Out[4]: ((4, 5, 6), (7, 8))

A tuple can be converted to any sequence or iterator tuples:

1 In [5]: tuple([4, 0, 2])
2 Out[5]: (4, 0, 2)
3 In [6]: tup = tuple('string')
4 In [7]: tup
5 Out[7]: ('s', 't', 'r', 'i', 'n', 'g')

Access elements in square brackets can be used tuple

1 In [8]: tup[0]
2 Out[8]: 's'

If the tuples in an object variable, such as a list, can be modified in situ:

1 In [9]: tup = tuple(['foo', [1, 2], True])
2 In [11]: tup[1].append(3)
3 In [12]: tup
4 Out[12]: ('foo', [1, 2, 3], True)

Addition operator can tuple in series:

1 In [13]: (4, None, 'foo') + (6, 0) + ('bar',)
2 Out[13]: (4, None, 'foo', 6, 0, 'bar')

Tuples multiplied by an integer, such as lists, tuples copy will be several in series

1 In [14]: ('foo', 'bar') * 4
2 Out[14]: ('foo', 'bar', 'foo', 'bar', 'foo', 'bar', 'foo', 'bar'
3 )

 

Split tuple
if you want to assign to variables similar tuples tuple, Python will try to split the value of the right of the equal sign :

. 1 the In [15]: TUP = (. 4,. 5,. 6 )
 2 the In [16]: A, B, C = TUP
 . 3 the In [. 17 ]: B
 . 4 Out [. 17]:. 5
 . 5  -tuple even when it contains tuple will be split:
 . 6 the In [18 is]: = TUP. 4,. 5, (. 6,. 7 )
 . 7 the In [. 19]: A, B, (C, D) = TUP
 . 8 the In [20 is ]: D
 . 9 Out [ 20]: 7

 

Using this feature, you can easily replace a variable name, other languages could be like this:
tmp = A
A = b
b = tmp
But in Python , the replacement can do this:

 1 In [21]: a, b = 1, 2
 2 In [22]: a
 3 Out[22]: 1
 4 In [23]: b
 5 Out[23]: 2
 6 In [24]: b, a = a, b
 7 In [25]: a
 8 Out[25]: 2
 9 In [26]: b
10 Out[26]: 1

Variables used to iteratively split tuple or sequence list:

1 In [27]: seq = [(1, 2, 3), (4, 5, 6), (7, 8, 9)]
2 In [28]: for a, b, c in seq:
3 ....: print('a={0}, b={1}, c={2}'.format(a, b, c))
4 a=1, b=2, c=3
5 a=4, b=5, c=6
6 a=7, b=8, c=9

Python recently added more advanced tuple split feature that allows tuples from the beginning of the " removal " of several elements.
It uses a special syntax * REST , which is also used in the function signature to grab any length list of positional parameters
number:

1 In [29]: values = 1, 2, 3, 4, 5
2 In [30]: a, b, *rest = values
3 In [31]: a, b
4 Out[31]: (1, 2)
5 In [32]: rest
6 Out[32]: [3, 4, 5]

rest part is the part you want to give up, rest of the name is not important. As usual wording, many Python
programmers will not need to use an underscore variables:

In [33]: a, b, *_ = values 

 

tuple method
because of the size and content of the tuple can not be modified, it is very lightweight instance methods. One very useful to
be COUNT (also applies to lists), it may be worth a statistical frequency of occurrence  :

1 In [34]: a = (1, 2, 2, 2, 3, 4, 2)
2 In [35]: a.count(2)
3 Out[35]: 4

 

2. List


Compared with the tuple, a variable length list, content may be modified. You can define the square brackets, or
use list functions:


 

Guess you like

Origin www.cnblogs.com/ucasljq/p/11627734.html