15-python_内建函数-string-list

string处理
 - 针对对象操作
    - str.capitalize()
    - str.replace()
    - str.split()
 - import string
    - string.replace()
list处理
 - len()
 - max()
 - min()
 - filter()
 - zip()
 - map()
 - reduce()

1. str.capitalize() 首字母大写

    capitalize(...)
        S.capitalize() -> string

        Return a copy of the string S with only its first character
        capitalized.
    >>> 
    >>> "abc".capitalize()
    'Abc'

2. str.replace() 替换

    replace(...)
        S.replace(old, new[, count]) -> string

        Return a copy of string S with all occurrences of substring
        old replaced by new.  If the optional argument count is
        given, only the first count occurrences are replaced.
    >>> 
    >>> "1999-09-19".replace("-", "/")
    '1999/09/19'
    >>> "1999-09-19".replace("-", "/", 1)
    '1999/09-19'

3. str.split() 分割,切割

    split(...)
        S.split([sep [,maxsplit]]) -> list of strings

        Return a list of the words in the string S, using sep as the
        delimiter string.  If maxsplit is given, at most maxsplit
        splits are done. If sep is not specified or is None, any
        whitespace string is a separator and empty strings are removed
        from the result.
    >>>
    >>> "1999-09-19".split()
    ['1999-09-19']
    >>> "1999-09-19".split("-")
    ['1999', '09', '19']
    >>> "1999-09-19".split("-",1)
    ['1999', '09-19']    

4. string.replace()

    >>> import string
    >>> help(string.replace)
    Help on function replace in module string:

    replace(s, old, new, maxreplace=-1)
        replace (str, old, new[, maxreplace]) -> string

        Return a copy of string str with all occurrences of substring
        old replaced by new. If the optional argument maxreplace is
        given, only the first maxreplace occurrences are replaced.
    >>>
    >>> string.replace("192.168.10.110", ".", "-")
    '192-168-10-110'

5. filter(), 保留符合条件的元素

    filter(...)
        filter(function or None, sequence) -> list, tuple, or string

        Return those items of sequence for which function(item) is true.  If
        function is None, return the items that are true.  If sequence is a tuple
        or string, return the same type, else return a list.
    >>>
    >>> filter( lambda x: x > "1", "0123" )
    '23'
    >>> filter( lambda x: x > 1, ( 0, 1, 2, 3 ) )
    (2, 3)
    >>> filter( lambda x: x > 1, range(-1, 4) )
    [2, 3]

6. zip() 合并两个sequence

    zip(...)
        zip(seq1 [, seq2 [...]]) -> [(seq1[0], seq2[0] ...), (...)]

        Return a list of tuples, where each tuple contains the i-th element
        from each of the argument sequences.  The returned list is truncated
        in length to the length of the shortest argument sequence.
    >>>
    >>> zip( (1, 2), (1, 2, 3) )
    [(1, 1), (2, 2)]
    >>> zip( [1, 2, 3], [1, 2]  )
    [(1, 1), (2, 2)]

7. map() 将sequence转成list,

    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).
    >>>
    >>> map(None, [1,2,3], ['a', 'b'])
    [(1, 'a'), (2, 'b'), (3, None)]    
    >>> map(lambda x : x, "123")
    ['1', '2', '3']
    >>> map(lambda x,y : x+y, [1,2,3], [10,20,30])
    [11, 22, 33]
    

8. zip() 与 map() 的异同

  -当map中function为None时, map()的功能与zip()一致, 都是并行遍历.
   不同的是map()以最长的序列为准, 不足的补None,而zip()以最短的为准.
  -当map中function不为None时, 会依次从各个sequence中取相同index的值做运算.

9. reduce() 依次取出每个item

    reduce(...)
        reduce(function, sequence[, initial]) -> value

        Apply a function of two arguments cumulatively to the items of a sequence,
        from left to right, so as to reduce the sequence to a single value.
        For example, reduce(lambda x, y: x+y, [1, 2, 3, 4, 5]) calculates
        ((((1+2)+3)+4)+5).  If initial is present, it is placed before the items
        of the sequence in the calculation, and serves as a default when the
        sequence is empty.
    >>># 4!
    >>> reduce(lambda x,y : x*y, [1, 2, 3, 4])
    24
    >>># 1+2+...+99+100
    >>> reduce(lambda x,y : x+y, xrange(1, 101))
    5050
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值