python map、join函數


map() 會根據提供的函數對指定序列做映射。

第一個參數 function 以參數序列中的每一個元素調用 function 函數,返回包含每次 function 函數返回值的新列表。

map(function, iterable, ...)

其中

  • function -- 函數,有兩個參數
  • iterable -- 一個或多個序列
  1.  
    >>> def square(x) : # 計算平方數... return x ** 2...
  2.  
    >>> map(square, [1,2,3,4,5]) # 計算列表各個元素的平方
  3.  
    [ 1, 4, 9, 16, 25]
  4.  
    >>> map(lambda x: x ** 2, [1, 2, 3, 4, 5]) # 使用 lambda 匿名函數
  5.  
    [ 1, 4, 9, 16, 25]
  6.  
    # 提供了兩個列表,對相同位置的列表數據進行相加
  7.  
    >>> map(lambda x, y: x + y, [1, 3, 5, 7, 9], [2, 4, 6, 8, 10])
  8.  
    [ 3, 7, 11, 15, 19]

 join() 方法用於將序列中的元素以指定的字符連接生成一個新的字符串。

str.join(sequence)
  1.  
    str = "-";
  2.  
    seq = ( "a", "b", "c"); # 字符串序列
  3.  
    print str.join( seq );

split()通過指定分隔符對字符串進行切片,如果參數num 有指定值,則僅分隔 num 個子字符串

str.split(str="", num=string.count(str))

 

  • str -- 分隔符,默認為所有的空字符,包括空格、換行(\n)、制表符(\t)等。
  • num -- 分割次數。
  1.  
    str = "this is string example....wow!!!"
  2.  
    print (str.split( ))
  3.  
    print (str.split('i',1))
  4.  
    print (str.split('w'))
  5.  
     
  6.  
    [ 'this', 'is', 'string', 'example....wow!!!']
  7.  
    [ 'th', 's is string example....wow!!!']
  8.  
    [ 'this is string example....', 'o', '!!!']

 

data_4temp1['amount2'] = data_4temp1['amount'].map(lambda x: int(''.join(x[1:].split(','))))


原文地址:https://blog.csdn.net/abcdrachel/article/details/80520484


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM