定位字符、字符串截取

Python基础篇【定位字符串位置、字符串截取】

字符串格式化函数 str.format()

1
2
3
4
5
6
7
8
9
10
11
12
# 直接赋值
print("姓名:{},地址:{}".format("王小二", "山东青岛"))
# 直接赋值
print("姓名:{name}, 地址 {adress}".format(name="王小二", adress="山东青岛"))
# 通过字典设置参数
site = {"name": "王小二", "adress": "山东青岛"}
print("姓名:{name}, 地址:{adress}".format(**site))
# 通过列表索引设置参数,"0" 是必须的
arr = ['王小二', '山东青岛']
print("姓名:{0[0]}, 地址:{0[1]}".format(arr))
# 数字格式化
~ ~ ~ ~

循环遍历

遍历一个范围内的数字

1
2
for i in xrange(6):
print i ** 2

xrange会返回一个迭代器,用来一次一个值地遍历一个范围,这种方式比range更省内存。在python3中xrange已经改名为range。

遍历集合

1
2
3
colors = ['red', 'green', 'blue', 'yellow']
for color in colors:
print color

反向遍历集合

1
2
for color in reversed(colors):
print color

遍历集合及其下标

1
2
for i, color in enumerate(colors):
print i, '-->', color

遍历两个集合

1
2
3
4
names = ['raymond', 'rachel', 'mattthew']
colors = ['red', 'green', 'blue', 'yellow']
for name, color in izip(names, colors):
print name, '-->', color

zip在内存中生成一个新的列表,需要更多的内存,izip比zip效率更高。在python3中,izip改名为zip,替换了原来的zip成为内置函数。

有序遍历

1
2
3
4
5
colors = ['red', 'green', 'blue', 'yellow']
for color in sorted(colors):
print color
for color in sorted(coloes, reverse = True):
print color

自定义排序顺序

1
2
colors = ['red', 'green', 'blue', 'yellow']
print sorted(colors, key=len)

列表解析和生成器

1
print sum(i ** 2 for i in xrange(10))

在循环内识别多个退出点

1
2
3
4
5
6
7
def find(seq, target):
for i, value in enumerate(seq):
if value == target:
break
else:
return -1
return i

分离临时上下文

1
2
3
with open('help.txt', 'w') as f:
with redirect_stdout(f):
help(pow)

上述代码用于演示如何临时把标准输出重定向到一个文件,然后再恢复正常。注意redirect_stdout在python3.4加入。

打开关闭文件

1
2
with open('data.txt') as f:
data = f.read()

使用锁

1
2
3
4
lock = threading.Lock()
with lock:
print 'critical section 1'
print 'critical section 2'

用字典计数

1
2
3
4
5
6
7
colors = ['red', 'green', 'red', 'blue', 'green', 'red']
d = {}
for color in colors:
d[color] = d.get(color, 0) + 1
d = defaultdict(int)
for color in colors:
d[color] += 1

获取日期时间、处理日期时间

Python基础篇【获取日期、处理日期】