python list tuples and advanced operations

Advanced Operations List

First, traverse the list

>>> ls=['a','d','it']
>>> for val in ls:
...     print (val)
... 
a
d
it

for a colon followed, for the first loop line is four spaces for all indentation python, use four spaces, if you use the editor, for convenience, we may use the Tab tabs instead of spaces, but Tab replace tabs provided with four spaces, the editor will typically have this feature.

Second, create a list of values

1, the range method using almost any form to create the list of values, a simple example is as follows:

>>> for val in range(1,5):
...     print (val)
... 
1
2
3
4

After the package does not include the front range method, this method and many similar programming languages.

2, although the above-described example, may be directly recycled to obtain the value of each element in the list range distribution, but not the range (l, 5) directly assigned to a variable, it is necessary to use the list method to convert

Directly to the range (1,5) assigned to a variable, the result is not what we want the output list

>>> myls=range(1,5)
>>> myls
range(1, 5)

Use lsit converted to a list

>>> myls=list(range(1,5))
>>> myls
[1, 2, 3, 4]

3, using the step size range can also be specified, such as to generate a arithmetic arithmetic List 3

>>> eqdiffls=list(range(1,50,3))
>>> eqdiffls
[1, 4, 7, 10, 13, 16, 19, 22, 25, 28, 31, 34, 37, 40, 43, 46, 49]

Third, a simple list of statistical calculations, such as obtaining the maximum value, minimum value, and summation

>>> countls=list(range(1,10))
>>> countls
[1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> min(countls)
1
>>> max(countls)
9
>>> sum(countls)
45

Fourth, the list comprehension

First clear look, python calculated in square format val ** 2, 3 if you want to calculate the power of 2 can be written as 2 ** 3

>>> 4**2
16
>>> 2**3
8

If you want to generate a set of squared values ​​of a set of numbers, we may need to do this, such as the square of the list of values ​​1-4

>>> squarels=[]
>>> for val in range(1,5):
...     squarels.append(val**2)
... 
>>> squarels
[1, 4, 9, 16]

List generated in this manner requires four lines, there is a list of the above-described embodiment can generate the desired line of code, this embodiment is the list of parsed.

>>> del squarels
>>> squarels=[val**2 for val in range(1,5)]
>>> squarels
[1, 4, 9, 16]

Fifth, the list sliced

Format: list [startIndex: endIndex], also belong to the package before the package is not operating, the slice generator a new list, but will not have any impact on the previous list element

>>> squarels
[1, 4, 9, 16]
>>> squarels[1:3]
[4, 9]

Interception from one end position to the list index position

>>> squarels[2:]
[9, 16]

Interception from the start position to a location

>>> squarels[:2]
[1, 4]

If startIndex and endIndex are not entered, the equivalent of a copy of the list

>>> squarels[:]
[1, 4, 9, 16]

List Copy The copy method may also be used, easier to read the code

>>> squarels.copy()
[1, 4, 9, 16]

Traverse sections, and traverse the list the same way as

>>> for val in squarels[1:3]:
...     print (val)
... 
4
9

Tuple

Tuple is relatively simple, and the difference list is initialized when using parentheses (), using the list initialization brackets []; tuple elements can not be modified, to modify the list of elements allow

An initialization tuple

Tuples with parentheses () to initialize

>>> tup=()
>>> tup=(1,2,3)

Second, the traversal, consistent with the list

>>> for val in tup:
...     print (val)
... 
1
2
3

Third, the variables can not be modified tuple, then the modified error

>>> tup[1]=1
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: 'tuple' object does not support item assignment

four. Although tuple elements can not be modified, but allows you to modify the tuple itself

>>> tup=(12)
>>> tup=(12,3)
>>> 

 

Guess you like

Origin www.cnblogs.com/qq931399960/p/11100950.html