python内建函数之string与list的用法举例

发布时间:2020-03-07编辑:脚本学堂
本文介绍下,python中的二个内建函数string与list的用法,包括首字母大写、字符串的替换、字符串的分割等操作,有需要的朋友参考下吧。

本节主要内容:
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