您的位置:首页 > 编程语言 > Python开发

python标准库

2016-12-10 20:24 337 查看
zip
([iterable, ...])
This function returns a list of tuples, where the i-th tuple contains the i-th element from each of the argument sequences or iterables. The returned list is truncated in length to the length of the shortest argument sequence. When there are multiple arguments which are all of the same length,
zip()
is similar to
map()
with an initial argument of
None
. With a single sequence argument, it returns a list of 1-tuples. With no arguments, it returns an empty list.

The left-to-right evaluation order of the iterables is guaranteed. This makes possible an idiom for clustering a data series into n-length groups using
zip(*[iter(s)]*n)
.

返回一个列表,列表的元素是元组,元组的元素是一个集合,第 i 个集合包含(参数传入的)若干个可迭代对象第 i 个位置的元素:

>>> zip(((1,2),5),[3,4])
[((1, 2), 3), (5, 4)]

返回的列表的长度取决于参数中最短的可迭代对象:

>>> zip((1,2,5),[3,4])
[(1, 3), (2, 4)]

当可迭代对象的长度一致,zip和调用None的map结果一样:

>>> map(None,[1,2,5],[3,4])
[(1, 3), (2, 4), (5, None)]
>>> map(None,[1,2],[3,4])
[(1, 3), (2, 4)]
>>> zip([1,2],[3,4])
[(1, 3), (2, 4)]

当只使用一个可迭代对象时,返回列表的每个元组一一对应于可迭代对象的每个元素:

>>> zip((1,2,(3,4)))
[(1,), (2,), ((3, 4),)]

没有参数直接调用返回一个空列表。

在zip中若干个可迭代对象从左到右被计算:

>>> [(1,2)*2,(3,4)*2]
[(1, 2, 1, 2), (3, 4, 3, 4)]
>>> zip(*((1,2),(3,4))*2)
[(1, 3, 1, 3), (2, 4, 2, 4)]

>>> zip(*((1,2),(3,4)))*2
[(1, 3), (2, 4), (1, 3), (2, 4)]
>>> [(1,2),(3,4)]*2
[(1, 2), (3, 4), (1, 2), (3, 4)]

what's news in 3.6:

map
(function, iterable, ...)
Apply function to every item of iterable and return a list of the results. If additional iterable arguments are passed, function must take that many arguments and is applied to the items from all iterables in parallel. If one iterable is shorter than another it is assumed to be extended with
None
items. If function is
None
, the identity function is assumed; if there are multiple arguments,
map()
returns a list consisting of tuples containing the corresponding items from all iterables (a kind of transpose operation). The iterable arguments may be a sequence or any iterable object; the result is always a list.

基本顺序类型操作(即可以同时支持可变和不可变顺序类型)

OperationResultNotes
x in s
True
if an item of s is equal to x, else
False
(1)
x not in s
False
if an item of s is equal to x, else
True
(1)
s + t
the concatenation of s and t(6)(7)
s * n
or
n * s
equivalent to adding s to itself n times(2)(7)
s[i]
ith item of s, origin 0(3)
s[i:j]
slice of s from i to j(3)(4)
s[i:j:k]
slice of s from i to j with step k(3)(5)
len(s)
length of s
min(s)
smallest item of s
max(s)
largest item of s
s.index(x[, i[, j]])
index of the first occurrence of x in s (at or after index i and before index j)(8)
s.count(x)
total number of occurrences of x in s
可变顺序类型操作

OperationResultNotes
s[i] = x
item i of s is replaced by x
s[i:j] = t
slice of s from i to j is replaced by the contents of the iterable t
del s[i:j]
same as
s[i:j] = []
s[i:j:k] = t
the elements of
s[i:j:k]
are replaced by those of t
(1)
del s[i:j:k]
removes the elements of
s[i:j:k]
from the list
s.append(x)
appends x to the end of the sequence (same as
s[len(s):len(s)] = [x]
)
s.clear()
removes all items from
s
(same as
del s[:]
)
(5)
s.copy()
creates a shallow copy of
s
(same as
s[:]
)
(5)
s.extend(t)
or
s += t
extends s with the contents of t (for the most part the same as
s[len(s):len(s)] = t
)
s *= n
updates s with its contents repeated n times(6)
s.insert(i, x)
inserts x into s at the index given by i (same as
s[i:i] =[x]
)
s.pop([i])
retrieves the item at i and also removes it from s(2)
s.remove(x)
remove the first item from s where
s[i] == x
(3)
s.reverse()
reverses the items of s in place(4)
JSON

使用json.dumps将python对象转换为JSON格式,例如:

>>> import json
>>> print json.dumps({'a': 'Runoob', 'b': 7}, sort_keys=True, indent=4, separators=(',', ': '))
{
"a": "Runoob",
"b": 7
}

separators=(item_separator, key_separator)

python 原始类型向 json 类型的转化对照表:

PythonJSON
dictobject
list, tuplearray
str, unicodestring
int, long, floatnumber
Truetrue
Falsefalse
Nonenull
json 类型转换到 python 的类型对照表:

JSONPython
objectdict
arraylist
stringunicode
number (int)int, long
number (real)float
trueTrue
falseFalse
nullNone
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: