Python learning notes (3) and python learning notes
1. Slice
The Slice operation is similar to subList in java, that is, obtaining a subList
For example, if L = ['hangsan', 'lisi', 'hangzhou'], then L [0, 2] indicates that the index is retrieved from 0 until index 2 (excluding 2 ), exactly two elements. If the first index is 0, it can be omitted.
Next we will create a series from 0 to 99: L = range (100)
1. Obtain the first 10 numbers: L [: 10]
2. Obtain the last 10 digits: L [-10:] # the index of the last element is-1.
3. Obtain the first 11-20 numbers: L []
4. Obtain the first 10 numbers. Take one for each two numbers: L [: 10: 2].
5. For each of the five numbers, take one: L [: 5].
Tuple, string 'xxx' or Unicode string u'xxx' can also be sliced
Ii. Iteration
Python's for loop can be used not only on list or tuple, but also on other iteratable objects.
1. Iterative dict:
d = {'a': 1, 'b': 2, 'c': 3}for key in d: print keyThe storage of dict is not arranged in the order of list, so the order of iteration results may be different.
By default, key is iterated by dict. If you want to iterate value, you can use for value in d. itervalues (). If you want to iterate key and value at the same time, you can use for k, v in d. itervalues ()
2. Determine whether objects can be iterated: (use the iterable type of the collections module to determine)
From collections import Iterableprint isinstance ('abc', Iterable) # Can str be iterated (True) print isinstance ([, 3], Iterable) # can list be iterated (True) print isinstance (123, Iterable) # Whether the integer can be iterated (False)3. Iterative indexing and element itself: (use the enumerate function to convert the list into an index-element pair)
>>> for i, value in enumerate(['A', 'B', 'C']):... print i, value...0 A1 B2 C
Iii. List generation
1. Generate a list
>>> [x * x for x in range(1, 11)][1, 4, 9, 16, 25, 36, 49, 64, 81, 100]
2. Generate a list with conditions: (filter out the even square)
>>> [x * x for x in range(1, 11) if x % 2 == 0][4, 16, 36, 64, 100]
3. Double-layer cyclic generation in full order:
>>> [m + n for m in 'ABC' for n in 'XYZ']['AX', 'AY', 'AZ', 'BX', 'BY', 'BZ', 'CX', 'CY', 'CZ']
4. generate a list of multiple variables
>>> d = {'x': 'A', 'y': 'B', 'z': 'C' }>>> for k, v in d.iteritems():... print k, '=', v... y = Bx = Az = C
Iv. Generator
List is not a one-time generation, but the subsequent elements are constantly computed in the Process of loop. This kind of mechanism for calculating one side of loop is called generator.
L = [x * x for x in range (10)] # list-brackets g = (x * x for x in range (10) # generator-parentheses
Another method for defining generator:
If a function definition contains the yield keyword, this function is no longer a common function, but a generator.
The generator function is executed every time next () is called. When the yield statement is returned, the yield statement returned last time is executed again.
For example, define a generator and return numbers 1, 3, and 5 in sequence.
def odd(): print 'step 1' yield 1 print 'step 2' yield 3 print 'step 3' yield 5o = odd()print o.next() # step1 1print o.next() # step2 2 print o.next() # step3 3