Python关键知识点整理(一)
Build-in Data Sturcture
使用List的几点注意
- insert 是一个计算复杂度较高的操作,尽量使用append方法。因为插入点之后的元素,都需要向后移动。如果需要在队列头、尾插入数据,可以使用collections.deque (a double-ended queue)。
b_list=[2,3,None,5]
b_list.append(1, 'red') # do not use this.
- 队列可以使用+,实现list concatenates。但是list concatenation是一个高复杂度的操作,因为会生产一个新list,同时把元素拷贝过去。建议使用extend方法替代。
everything = []
for chunk in list_of_lists:
everything.extend(chunk)
优于
everything = []
for chunk in list_of_lists:
everything = everything + chunk
Build-in Sequence function
enumerate:
用法:
for i, value in enumerate(collection):
# do something with value
等价于
i = 0
for value in collection:
# do something with value
i += 1
In [83]: some_list = ['foo', 'bar', 'baz']
In [84]: mapping = {}
In [85]: for i, v in enumerate(some_list):
....: mapping[v] = i
In [86]: mapping
Out[86]: {'bar': 1, 'baz': 2, 'foo': 0}
sorted:
返回一个新的list。和List的sort方法参数相同。
In [87]: sorted([7, 1, 2, 6, 0, 3, 2])
Out[87]: [0, 1, 2, 2, 3, 6, 7]
In [88]: sorted('horse race')
Out[88]: [' ', 'a', 'c', 'e', 'e', 'h', 'o', 'r', 'r', 's']
zip:
“pairs” up the elements of a number of lists, tuples, or other sequences to create a list of tuples.
In [89]: seq1 = ['foo', 'bar', 'baz']
In [90]: seq2 = ['one', 'two', 'three']
In [91]: zipped = zip(seq1, seq2)
In [92]: list(zipped)
Out[92]: [('foo', 'one'), ('bar', 'two'), ('baz', 'three')]
Collection Comprehension
[exp for val in collection if condition]
注意,是[]括号。如果是(),则变成了generator expression。
例子
In [164]: some_tuples = [(1, 2, 3), (4, 5, 6), (7, 8, 9)]
In [165]: flattened = [x for tup in some_tuples for x in tup]
In [166]: flattened
Out[166]: [1, 2, 3, 4, 5, 6, 7, 8, 9]
等价于
flattened = []
for tup in some_tuples:
for x in tup:
flattened.append(x)
函数式编程的典型例子
# input, process these string array.
In [171]: states = [' Alabama ', 'Georgia!', 'Georgia', 'georgia', 'FlOrIda',
.....: 'south carolina##', 'West virginia?']
常规做法:
import re
def clean_strings(strings):
result = []
for value in strings:
value = value.strip()
value = re.sub('[!#?]', '', value)
value = value.title()
result.append(value)
return result
函数式做法:
def remove_punctuation(value):
return re.sub('[!#?]', '', value)
clean_ops = [str.strip, remove_punctuation, str.title]
def clean_strings(strings, ops):
result = []
for value in strings:
for function in ops: # function usage
value = function(value)
result.append(value)
return result
结果一致:
In [175]: clean_strings(states, clean_ops)
Out[175]:
['Alabama',
'Georgia',
'Georgia',
'Georgia',
'Florida',
'South Carolina',
'West Virginia']
函数也能作为参数被其它函数调用,如:
In [176]: for x in map(remove_punctuation, states):
.....: print(x)
Generators
示例:
def squares(n=10):
print('Generating squares from 1 to {0}'.format(n ** 2))
for i in range(1, n + 1):
yield i ** 2 # use yield to produce results
Generator return a sequence of multiple results lazily, pausing after each one until the next one is required.
Normal functions execute and return a single result at a time.
In [186]: gen = squares()
In [187]: gen
Out[187]: <generator object squares at 0x7fbbd5ab4570>
In [188]: for x in gen:
.....: print(x, end=' ')
Out[188]: 1 4 9 16 25 36 49 64 81 100
Generator expressions
和List comprehension格式很像,但是使用的是()。以下两种写法等价:
In [189]: gen = (x ** 2 for x in range(100))
In [190]: gen
Out[190]: <generator object <genexpr> at 0x7fbbd5ab29e8>
def _make_gen():
for x in range(100):
yield x ** 2
gen = _make_gen()
Generator expression的常见用法:
In [191]: sum(x ** 2 for x in range(100))
Out[191]: 328350
In [192]: dict((i, i **2) for i in range(5))
Out[192]: {0: 0, 1: 1, 2: 4, 3: 9, 4: 16}
itertools module
In [193]: import itertools
In [194]: first_letter = lambda x: x[0]
In [195]: names = ['Alan', 'Adam', 'Wes', 'Will', 'Albert', 'Steven']
In [196]: for letter, names in itertools.groupby(names, first_letter):
.....: print(letter, list(names)) # names is a generator
Out[196]:
A ['Alan', 'Adam']
W ['Wes', 'Will']
A ['Albert']
S ['Steven']
具体内容可以参见 Python Documentation–itertools