python内置方法、模块讲解(十九)

  • 2016-05-18 21:23:28
  • 1366
  • 0

五十、map方法

1、定义:

map(...)
    map(function, sequence[, sequence, ...]) -> list

    Return a list of the results of applying the function to the items of
    the argument sequence(s).  If more than one sequence is given, the
    function is called with an argument list consisting of the corresponding
    item of each sequence, substituting None for missing values when not all
    sequences have the same length.  If the function is None, return a list of
    the items of the sequence (or a list of tuples if more than one sequence).

2、解释:返回由function方法处理过sequence每个元素的列表。若传入多个sequence时, 则分别从这些sequence中取出同序列号的元素作为一个list传入到function中。

                对于长度不够的sequence 以None代替从该sequence中取出的值。如果function为None, 则返回由这些sequence中取出同序列号的元素组成的list。

                ps:此方法用处较大, 且适当运用能提高的代码的运行效率, 建议掌握。

3、举例:

>>> map(None, "fdafd", range(10))
[('f', 0), ('d', 1), ('a', 2), ('f', 3), ('d', 4), (None, 5), (None, 6), (None, 7), (None, 8), (None, 9)]
>>> map(None, "fdafd")
['f', 'd', 'a', 'f', 'd']
>>> map(str, range(5))
['0', '1', '2', '3', '4']
>>> map(divmod, range(5,10), range(1,6))
[(5, 0), (3, 0), (2, 1), (2, 0), (1, 4)]

五十一、max方法

1、定义:

max(...)
    max(iterable[, key=func]) -> value
    max(a, b, c, ...[, key=func]) -> value

    With a single iterable argument, return its largest item.
    With two or more arguments, return the largest argument.

2、解释:此法顾名思义:取最大值。当传入一个可迭代参数时, 则取该参数中的最大值;当传入两个或者两个以上参数时, 则返回最大的那个参数。

3、举例:

>>> max([4,5,7,1])
7
>>> max(3,5,7,1)
7
 


发表评论

* *