1、字典
字典(dict)可算是Python最重要的内置数据结构,更常见的名字是哈希映射和相连数组。是一种大小可变的键值对集其中的键和值都是value对象。创建字典的方式之一是:使用大括号和冒号分割键和值。
keys和values方法是获取键和值的列表。虽然键值对没有特定的顺序,但这两个函数会以相同的顺序输出键和值
2、嵌套列表推导式
假设想获取带有两个或两个以上的字母e的名字,并将它们放入一个新的列表中,可以使用for循环来实现
all_data = [['Tom', 'Billy', 'Jefferson', 'Andrew', 'Wesley', 'Steven', 'Joe'],
['Susie', 'Casey', 'Jill', 'Ana', 'Eva', 'Jeneifer', 'Stephnie']]
names_of_interest = []
for names in all_data:
enough_es = [name for name in names if name.count('e') >= 2]
names_of_interest.extend(enough_es)
print names_of_interest
整个运算过程也可以写成一条嵌套列表推导式
all_data = [['Tom', 'Billy', 'Jefferson', 'Andrew', 'Wesley', 'Steven', 'Joe'],
['Susie', 'Casey', 'Jill', 'Ana', 'Eva', 'Jeneifer', 'Stephnie']]
result = [name for names in all_data for name in names if name.count('e') >=2]
print result
3、 生成器
能以一种一致的方式对序列进行迭代是Python的一个重要的特点。这是通过一种叫做迭代器的协议,它是一种是对象可迭代的通用方式的方式实现。比如说,字典进行迭代可以得到其所有的键
some_dict = {'a':1, 'b':2, 'c':3}
for key in some_dict:
print key
a
b
c
some_dict = {'a':1, 'b':2, 'c':3}
for key in some_dict:
print key,
```
输出结果:a b c
注意逗号的作用,打印输出不换行。
迭代器是一种特殊的对象,它可以在诸如循环之类的上下文中向Python解释器输送对象
生成器是构造新的迭代对象风一种简单方式。一般的函数执行之后只会返回单个值,而生成器则是以延迟的方式返回一值序列,即每返回一个值后暂停,直到下一个值被请求时再继续。要创建一个生成器,只需将函数中的return替换为yeild
```python
def make_change(amount, coins=[1, 5, 10, 25], hand = None):
hand = [] if hand is None else hand
if amount == 0:
yield hand
for coin in coins:
if coin > amount or (len(hand)) > 0 and hand[-1] < coin:
continue
for result in make_change(amount - coin, coins=coins, hand=hand + [coin]):
yield result
for way in make_change(100, coins=[10, 25, 50]):
print way
print len(list(make_change(100)))
<div class="se-preview-section-delimiter"></div>
4、生成器表达式
生成器表达式是构造生成器的简单方式。生成器也有一个类似与列表、字典、集合推导的东西,其创建方式为,把列表推导式两端的方括号改成圆括号
5、文件和操作系统
path = 'C:\Users\wt\Desktop\'
with open('爬虫.txt', 'w') as handle:
handle.writelines(x for x in open(path) if len(x) > 1)
open('C:\Users\wt\Desktop\爬虫.txt').readlines()
<div class="se-preview-section-delimiter"></div>
在执行上述代码时出现了如下错误
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "D:\anzhuang\Anaconda\lib\site-packages\spyderlib\widgets\externalshell\sitecustomize.py", line 682, in runfile
execfile(filename, namespace)
File "D:\anzhuang\Anaconda\lib\site-packages\spyderlib\widgets\externalshell\sitecustomize.py", line 71, in execfile
exec(compile(scripttext, filename, 'exec'), glob, loc)
File "C:/Users/wt/.spyder2/template.py", line 7
path = 'C:\Users\wt\Desktop\'
^
SyntaxError: EOL while scanning string literal
>>> SyntaxError: EOL while scanning string literal
网上提到文本文件中没有引号
在执行上述代码时出现了如下错误
```python
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "D:\anzhuang\Anaconda\lib\site-packages\spyderlib\widgets\externalshell\sitecustomize.py", line 682, in runfile
execfile(filename, namespace)
File "D:\anzhuang\Anaconda\lib\site-packages\spyderlib\widgets\externalshell\sitecustomize.py", line 71, in execfile
exec(compile(scripttext, filename, 'exec'), glob, loc)
File "C:/Users/wt/.spyder2/template.py", line 7
path = 'C:\Users\wt\Desktop\'
^
SyntaxError: EOL while scanning string literal
>>> SyntaxError: EOL while scanning string literal
网上提到文本文件中没有引号
后来,发现是编码的问题,由于文件是以中文命名的,而且文件也是中文。