0. 函数定义
map(function, iterable, ...) :
- map() 会根据提供的函数对指定序列做映射。
- 第一个参数 function 以参数序列中的每一个元素调用 function 函数,返回包含每次 function 函数返回值的新列表。
sum(iterable[, start])
- sum() 方法对序列进行求和计算
- iterable – 可迭代对象,如:列表、元组、集合。
- start – 指定相加的参数,如果没有设置这个值,默认为0。
- 一些用法:
>>>sum([0,1,2])
3
>>> sum((2, 3, 4), 1)
10
>>> sum([0,1,2,3,4], 2)
12
a = np.array([[1,2],[3,4]])
print(np.sum(a, axis=1, keepdims=True))
print(np.sum(a, axis=1))
array([[3], [7]])
array([3, 7])
1. sum函数对列表降维
def strToFloat(x) :
return float(x)
oldlist = [['12.3', '13.5', '13.14','520.521','38438', '2333']]
newlist = sum(oldlist,[])
print(newlist)
map(strToFloat,newlist)
print(list(map(float,newlist)))
x = [['123', '345', '456', '789', '112.231', '76653'], ['63', '88', '56', '78', '112.231', '545.8']]
xn = sum(x,[])
print(xn)
结果为:['123', '345', '456', '789', '112.231', '76653', '63', '88', '56', '78', '112.231', '545.8']
执行效果是 oldlist 中的子列表逐一与第二个参数相加,而列表的加法相当于 extend 操作,所以最终结果是由 [] 扩充成的列表。 这里有两个关键点:sum() 函数允许带两个参数,且第二个参数才是起点。 可能 sum() 函数用于数值求和比较多,然而用于作列表的求和,就有奇效。 这段话是引用
2. 用map将不同维度的列表中字符串转换为数字
一维:
oldlist = ['12.3', '13.5', '13.14','520.521','38438', '2333']
print(list(map(float,oldlist)))
结果为
[12.3, 13.5, 13.14, 520.521, 38438.0, 2333.0]
二维:
x = [['123', '345', '456', '789', '112.231', '76653'], ['63', '88', '56', '78', '112.231', '545.8']]
x = [list(map(float, item)) for item in x]
print(x)
oldlist = [['12.3', '13.5', '13.14','520.521','38438', '2333']]
newlist = sum(oldlist,[])
print(newlist)
print(list(map(float,newlist)))
三维:map 本身对列表中每一个元素进行操作,所以就相当于循环一次
oldlist1 = [[['2','4'],['2','3']],[['1','3'],['1','2']]]
newlist = np.asarray(oldlist1).astype(float).tolist()
print(newlist)
[[[2.0, 4.0], [2.0, 3.0]], [[1.0, 3.0], [1.0, 2.0]]]
x = [[['2', '3'], ['3', '4']]]
c = [[list(map(float, item)) for item in b] for b in (a for a in x)]
c = [[list(map(float, item)) for item in b] for b in x]
print(c)
[[[2.0, 3.0], [3.0, 4.0]]]
def cov(xxx):
res = []
for x_ in xxx:
res.append(list(map(float, x_)))
return res
oldlist1 = [[['2','4'],['2','3']],[['1','3'],['1','2']]]
newlist1 = list(map(cov,oldlist1))
print(newlist1)
[[[2.0, 4.0], [2.0, 3.0]], [[1.0, 3.0], [1.0, 2.0]]]
这些里面对2取余 是因为元素个数 可以再设置两个变量 获取长度
newx1 = []
newx2 = []
count3 = 0
print("尝试1:")
for it in oldlist1:
print(it)
for io in it:
print(io)
newx = list(map(float, io))
print(newx)
if(count3%2 == 0 ):
newx1 = []
newx1 = newx1 + [newx]
print(newx1)
count3 += 1
newx2 = newx2 + [newx1]
print(newx2)
[['2', '4'], ['2', '3']]
['2', '4']
[2.0, 4.0]
[[2.0, 4.0]]
['2', '3']
[2.0, 3.0]
[[2.0, 4.0], [2.0, 3.0]]
[[[2.0, 4.0], [2.0, 3.0]]]
[['1', '3'], ['1', '2']]
['1', '3']
[1.0, 3.0]
[[1.0, 3.0]]
['1', '2']
[1.0, 2.0]
[[1.0, 3.0], [1.0, 2.0]]
[[[2.0, 4.0], [2.0, 3.0]], [[1.0, 3.0], [1.0, 2.0]]]
slist = []
count = 0
count1 =0
sslist = []
ssslist = []
def te(x) :
global ssslist
for i in x:
global sslist
global count1
for item in i :
global slist
global count
newx =list(map(float, item))
print(newx)
if(count%2 == 0):
slist = []
slist = slist + newx
count += 1
print(sslist)
if(count1%2 == 0 ):
sslist = []
sslist = sslist + [slist]
count1 += 1
print(sslist)
print(count1)
if(count1%2 == 0):
ssslist = ssslist + [sslist]
print(ssslist)
oldlist1 = [[['2','4'],['2','3']],[['1','3'],['1','2']]]
newlist1 = list(map(te,oldlist1))
[2.0]
[4.0]
[]
[[2.0, 4.0]]
1
[2.0]
[3.0]
[[2.0, 4.0]]
[[2.0, 4.0], [2.0, 3.0]]
2
[[[2.0, 4.0], [2.0, 3.0]]]
[1.0]
[3.0]
[[2.0, 4.0], [2.0, 3.0]]
[[1.0, 3.0]]
3
[1.0]
[2.0]
[[1.0, 3.0]]
[[1.0, 3.0], [1.0, 2.0]]
4
[[[2.0, 4.0], [2.0, 3.0]], [[1.0, 3.0], [1.0, 2.0]]]
|