// 列表转置
a = [[1,2,3], [4,5,6]]
print (a)
#[[1, 2, 3], [4, 5, 6]]
print (map(list, zip(*a)))
#<map object at 0x00000221642BD108>
print (list(map(list, zip(*a))))
#[[1, 4], [2, 5], [3, 6]]
// map 映射
#map() 会根据提供的函数对指定序列做映射
map(function, iterable, ...)
def square(x) : # 计算平方数
return x ** 2
map(square, [1,2,3,4,5]) # 计算列表各个元素的平方
#[1, 4, 9, 16, 25]
// join 指定字符连接
a=['1','2','3','4','5']
b='hello world'
c=('1','2','3','4','5')
d={'name1':'a','name2':'b','name3':'c','name4':'d'}
'.'.join(a) #’1.2.3.4.5’
'-'.join(b) #’h-e-l-l-o- -w-o-r-l-d’
'-'.join(c) #’1-2-3-4-5’
'-'.join(d) #’name1-name2-name3-name4’
// 列表的str和int转换
a = [1,2,3]
b = [ str(i) for i in a ]
c = [ int(i) for i in b ]
// 2维列表取列
a=[[1,2,3], [4,5,6]]
b = [i[0] for i in a] # 从a中的每一行取第一个元素。
// 判断开头、结尾的字符,类似于正则
##仅用第一个参数即可,默认为全范围检测
str.startswith(substr, beg=0,end=len(string));
#str -- 检测的字符串。
#substr -- 指定的子字符串。
#beg -- 可选参数用于设置字符串检测的起始位置。
#end -- 可选参数用于设置字符串检测的结束位置
str.endswith(suffix[, start[, end]])
suffix -- 该参数可以是一个字符串或者是一个元素。
start -- 字符串中的开始位置。
end -- 字符中结束位置。
Python中的经常用,但经常忘记的命令函数(2)
最新推荐文章于 2022-03-16 09:59:04 发布