Sorted 排序
在Sorting Keys中:我们看到,此时排序过的L是仅仅按照第二个关键字来排的,如果我们想用第二个关键字 排过序后再用第一个关键字进行排序呢?
1
2
3
4
5
6
7
8
|
>>> L
=
[(
'd'
,
2
),(
'a'
,
4
),(
'b'
,
3
),(
'c'
,
2
)]
>>>
print
sorted
(L, key
=
lambda
x:(x[
1
],x[
0
]))
>>>[(
'c'
,
2
), (
'd'
,
2
), (
'b'
,
3
), (
'a'
,
4
)]
>>> test
{
'a'
: [
'b'
,
7
],
'c'
: [
'd'
,
5
],
'b'
: [
'c'
,
2
]}
>>>
sorted
(test.iteritems(), key
=
lambda
(x,y):operator.itemgetter(
1
)(y))
[(
'b'
, [
'c'
,
2
]), (
'c'
, [
'd'
,
5
]), (
'a'
, [
'b'
,
7
])]
|
快速生成指定大小文件:
1
2
3
|
with
open
(
'test.txt'
,
'wb'
) as f:
f.seek(
1024
*
*
2
-
1
)
f.write(
'\0'
)
|
通过字符串调用相关函数或方法
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
|
def
a1():
print
"a1_fun"
def
a2():
print
"a2_fun"
def
a3():
print
"a3_fun"
for
i
in
range
(
1
,
4
):
globals
()[
"a%d"
%
i]()
locals
()[
"a%d"
%
i]()
f
=
"abCDE"
for
i
in
[
"upper"
,
"lower"
]:
n
=
getattr
(
str
, i)
print
n(f)
# import os <==> __import__('os')
os_new
=
__import__
(
'os'
)
getcwd
=
getattr
(os_new,
'getcwd'
)
print
getcwd()
|
本文转自 nonono11 51CTO博客,原文链接:http://blog.51cto.com/abian/1217429,如需转载请自行联系原作者