目录

  • 1. 两个列表对应元素相减
  • 2. 创建相同值的数组/列表
  • 3. 对一个全为数字的列表做差分
  • 4. 绝对值
  • 5. 获取最大、最小值的索引


1. 两个列表对应元素相减

v1 = [21, 34, 45]
v2 = [55, 25, 77] 

v = list(map(lambda x: x[0]-x[1], zip(v2, v1)))
print("%s\n%s\n%s" %(v1, v2, v))

输出:

[21, 34, 45]
[55, 25, 77]
[34, -9, 32]

2. 创建相同值的数组/列表


a = [2] * 10
a
Out[3]: [2, 2, 2, 2, 2, 2, 2, 2, 2, 2]

# -----------------------------------------------------------
b = [1, 2] * 10
b
Out[5]: [1, 2, 1, 2, 1, 2, 1, 2, 1, 2, 1, 2, 1, 2, 1, 2, 1, 2, 1, 2]

# -----------------------------------------------------------
[[1, 2], [3, 4]] * 5
Out[7]: 
[[1, 2],
 [3, 4],
 [1, 2],
 [3, 4],
 [1, 2],
 [3, 4],
 [1, 2],
 [3, 4],
 [1, 2],
 [3, 4]]

3. 对一个全为数字的列表做差分

wo = [7, 5, 7, 3, 5, 1, 2]
diff = [ wo[i]-wo[i+1] for i in range(len(wo)-1) ]
print(diff)

[2, -2, 4, -2, 4, -1]

4. 绝对值

li = [-1,5,7,9,-10]
#map(function,data)
#需要使用list,不然返回的一个地址
print(type(map(abs,li)))
res = list(map(abs,li))
print(res)

type: <class ‘map’>
打印输出的结果为:[1,5,7,9,10]

5. 获取最大、最小值的索引

aa = [1,2,3,4,5]
aa.index(max(aa))

aa = [1,2,3,4,5]
aa.index(min(aa))