Python lists List

List of List allows Python data types Data Type more flexible. Values ​​in the list are called elements element or list item item.

Definition lists are as follows:

[ item1, item2, item3, ...]

data type of elements can be any of the list, even Nested list can be embedded, for example:

['Apple', 85, ['Big', 'Small'], 4.0]

The list can be viewed as a collection of Set.

1. subset sum

>>> A = [ 1, 2, 3 ]
>>> B = [ 4, 5, 6 ] 

>>> C = A + B
>>> C
[1, 2, 3, 4, 5, 6]

2. collection times

>>> C[1:3]
[2, 3]

3. Change the elements

>>> C[0] = 99

>>> C

[99, 2, 3, 4, 5, 6]

 4. traverse the list

>>> for i in C:
...    print (i)
...
99
2
3
4
5
6

5. Delete element

>>> C.pop(1)
2
>>> C
[99, 3, 4, 5, 6]

6. list of strings

String String is a sequence of characters and list List is a sequence of values.

>>> S = 'rice'
>>> T = list(S)
>>> T
['r', 'i', 'c', 'e']

 

>>> S = S + ' noodle'
>>> T = list(S)
>>> T
['r', 'i',

'C', 'e', ​​'', 'n', 'o', 'o', 'd', 'l', 'e']

Split into single letter characters

>>> S
'rice noodle'
>>> T= S.split()
>>> T
['rice', 'noodle'] 

 

>>> S = 'rice, noodle'
>>> T = S.split(',')
>>> T
['rice', ' noodle']

There regret: string S in rice noodle with extra spaces have, when transformed into List, comma separator delimiter is not removed, this extra white space.

>>> A = 'rice'
>>> B = 'rice'
>>> id(A)
37429464
>>> id(B)
37429464
>>> A is B
True

A and B are different look object Object, A and B are in fact the Pointer indicator, storing the same value 37429464, which is the address value points to 37,429,464, and the value 'rice', A and B so that reference the same object . However, the list is not the same:

>>> X = ['rice']
>>> Y = ['rice']
>>> id(X)
37364168
>>> id(Y)
34540488
>>> X is Y
False

X and Y are different from the list of objects which have the same list of two elements, the Equivalent are equal, but they are different objects, so it is not the same Identical.

 However, if we take the individual name Alias ​​Y to X is it?

>>> Y = X
>>> id(X)
37364168
>>> id(Y)
37364168
>>> X is Y
True

Here it is clear alias address Y of the same point X, they both point to the same object.

Secondly, the distinction modify the list of operations with the new list of operations is important. For example: append method to modify the list, however, the + operation to establish a new column respectively:

>>> Z = X.append('noodle')
>>> X
['rice', 'noodle']
>>> Z
>>> id(X)
37364168
>>> id(Z)
8791418547424
>>> Z
>>> X is Z
False

X object reference function append, after changing the stored value of the object, and not because the specified Assign to Z, and a new object Z.


>>> X.append('bread')
>>> X
['rice', 'noodle', 'bread']
>>> id(X)
37364168

 These are obvious examples too. However, the + operation is different, for example:

>>> Y = X + ['soup']
>>> X
['rice', 'noodle', 'bread']
>>> Y
['rice', 'noodle', 'bread', 'soup']
>>> id(X)
37364168
>>> id(Y)
34571912

 Use the + operation, the new object Y, and this action does not change the value of the original object X.

In fact Python also has a Call By Value (by value) and Call By Address (pass-by) this concept. Call By Value of operation will not change the value of the original object Object, and Call By Address value of the object will change. This concept Xiangdangchongyao .

 >>> def replace(X,Y):
...    Z = X
...    X = Y
...    Y = Z
...    print(X,Y)
...
>>> X = 2
>>> Y = 3
>>> replace(X,Y)
3 2
>>> X
2
>>> Y
3

Because Call By Value, function replace (X, Y) does not change the original X and Y values.

 >>> def replace_new(X,Y):
...     Z = X
...     X = Y
...     Y = Z
...     return X,Y

>>> X = 2
>>> Y = 3

>>> (X,Y) = replace_new(X,Y)
>>>
>>> X
3
>>> Y
2

Call By Reference Python support it? According to Reference [1] Python is not supported.

 

reference

1. Python: Call by Reference, https://stackoverflow.com/questions/47131458/python-call-by-reference

2. Facts and myths about Python names and values, https://nedbatchelder.com/text/names.html

/end

Guess you like

Origin www.cnblogs.com/chingchangmeng/p/11384428.html