python map、join函数

xiaoxiao2021-02-28  14

map() 会根据提供的函数对指定序列做映射。

第一个参数 function 以参数序列中的每一个元素调用 function 函数,返回包含每次 function 函数返回值的新列表。

map(function, iterable, ...)

其中

function -- 函数,有两个参数iterable -- 一个或多个序列 >>>def square(x) : # 计算平方数... return x ** 2... >>> map(square, [1,2,3,4,5]) # 计算列表各个元素的平方 [1, 4, 9, 16, 25] >>> map(lambda x: x ** 2, [1, 2, 3, 4, 5]) # 使用 lambda 匿名函数 [1, 4, 9, 16, 25] # 提供了两个列表,对相同位置的列表数据进行相加 >>> map(lambda x, y: x + y, [1, 3, 5, 7, 9], [2, 4, 6, 8, 10]) [3, 7, 11, 15, 19]

 join() 方法用于将序列中的元素以指定的字符连接生成一个新的字符串。

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

split()通过指定分隔符对字符串进行切片,如果参数num 有指定值,则仅分隔 num 个子字符串

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

 

str -- 分隔符,默认为所有的空字符,包括空格、换行(\n)、制表符(\t)等。num -- 分割次数。 str = "this is string example....wow!!!" print (str.split( )) print (str.split('i',1)) print (str.split('w')) ['this', 'is', 'string', 'example....wow!!!'] ['th', 's is string example....wow!!!'] ['this is string example....', 'o', '!!!']

 

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

 

 

 

 

 

 

 

转载请注明原文地址: https://www.6miu.com/read-1600342.html

最新回复(0)