Python (three)

slice

Use only one : , which means from beginning to end:

>>> L[:]
['Adam', 'Lisa', 'Bart', 'Paul']

The slice operation can also specify a third parameter: the third parameter means to take one every N, and the above L[::2] will take one out of every two elements, that is, take one every other element.

Replacing the list with a tuple, the slicing operation is exactly the same, but the result of the slicing also becomes a tuple.

iterate

The iterative operation is that for a collection, whether the collection is ordered or unordered, we can always take out each element of the collection in turn with the for loop.

Note: A set refers to a data structure that contains a set of elements. The ones we have introduced include:
1 Sorted sets: list, tuple, str and unicode;

2 unordered set: set
3 unordered set with key-value pairs: dict

Example: Please use a for loop to iterate over the sequence 1-100 and print out the multiples of 7.

for i in range(1, 100)[6::7]:
    print i

In Python, iteration always retrieves the element itself, not the index of the element.

To get the index, use the enumerate() function

>>> L = ['Adam', 'Lisa', 'Bart', 'Paul']
>>> for index, name in enumerate(L):
...     print index, '-', name
... 
0 - Adam
1 - Lisa
2 - Bart
3 - Paul

The enumerate() function turns :
['Adam', 'Lisa', 'Bart', 'Paul']
into something like:
[(0, 'Adam'), (1, 'Lisa'), (2, 'Bart'), (3, 'Paul')]
so each element of the iteration is actually a tuple:

for t in enumerate(L):
    index = t[0]
    name = t[1]
    print index, '-', name

Index iteration is not really accessed by index, but the enumerate() function automatically turns each element into a tuple such as (index, element), and then iterates to obtain the index and the element itself at the same time.

  1. The values() method actually converts a dict into a list of values.

  2. But the itervalues() method will not convert, it will take out the value from the dict in turn during the iteration, so the itervalues() method saves the memory required to generate the list than the values() method.

  3. print itervalues() and find that it returns a

If an object is said to be iterable, then we directly use the for loop to iterate it. It can be seen that iteration is an abstract data operation, and it does not have any requirements on the data inside the iterative object.

Find the symmetrical 3-digit number using a list comprehension with a 3-level for loop. For example, 121 is a symmetric number because it is 121 reversed from right to left.

Pay attention to the idea of ​​the three methods

L=[]
for x in range(1,10):
    for y in range(10):
        for z in range(1,10):
            if x==z :
                L.append(100*x+10*y+z)
print L

print [100 * x + 10 * y + z for x in range(1, 10) for y in range(10) for z in range(10) if x==z]

print [x for x in range(100,1000) if str(x)[0]==str(x)[-1]]

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324883331&siteId=291194637