考虑范围数组:
a = [[[4251, 4254], [4123, 4290]], [[4323, 4222], [4111, 4900]]]
另一个一维数组:
b = [1324, 2745]
我想通过将数组b
的第一个元素添加到数组a
中的所有第一组范围来创建一个新数组,并将数组b
的第二个元素添加到所有第二个元素数组a
中的范围集,即
[[[4251+1324, 4254+1324], [4123+1324, 4290+1324]], [[4323+2745, 4222+2745], [4111+2745, 4900+2745]]]
我怎样才能用Python做到这一点?
答案 0 :(得分:2)
这应该适用于任何长度(提供a
和b
具有相同的长度)
[[[i+b[k],j+b[k]] for (i,j) in a[k]] for k in range(len(a))]
在你的情况下:
>>> pprint([[[i+b[k],j+b[k]] for (i,j) in a[k]] for k in range(len(a))])
[[[7880.1484209792943, 8171.6175205036743],
[8225.6716915199249, 8378.3978601695999],
[4729.0665464257172, 4732.276168393144],
[5501.1899887058553, 5722.1839186014013],
[360.55565340157807, 365.04589505745309],
[20983.0, 20983.0],
[217.14888548659997, 232.6226734478],
[385.14888548659997, 400.62267344780003],
[2823.2942485733347, 2840.4346062020377],
[4502.0794526850004, 4700.9945330295996],
[10417.852743984931, 10986.246567556096],
[8191.6238503034892, 8194.8209578124479],
[29570.28005012768, 29570.662880091983]],
[[3250.5273987094843, 3252.7610916503263],
[5593.4838299153871, 5596.0104208719731],
[576.2276993911745, 622.11755144656183],
[3188.3893120098983, 3206.7951214629902],
[3246.2448304681798, 3250.0964343653186],
[3272.9341862125516, 3274.8892802899418],
[5969.1472437958755, 5971.2996518169357],
[4255.3500559453041, 4258.126659789853]]]
答案 1 :(得分:0)
这可以使用python中的list comprehension来实现。
a = [[1,2],[2,3],[3,4]]
b = [5,9]
[(x[0]+b[0], x[1]+b[1]) for x in a]
解释
for x in a
- 对列表a
进行迭代
x[0]
- 子列表的第一个元素
x[0]+b[0]
- 使用a
中的第一个元素添加b
中每个元素的第一个元素
x[1]+b[1]
- 将a
中每个元素的第二个元素添加到b
输出
[(6, 11), (7, 12), (8, 13)]
答案 2 :(得分:0)
您可以使用列表理解:
对a[0]
进行迭代,并为每组值添加b[0]
和b[1]
。
new_array = [(value[0] + b[0], value[1] + b[1]) for value in a[0]]
输出:
[(9202.148420979294, 10914.617520503674), (9547.671691519925, 11121.3978601696), (6051.066546425717, 7475.276168393144), (6823.189988705855, 8465.1839186014), (1682.5556534015782, 3108.045895057453), (22305.0, 23726.0), (1539.1488854866, 2975.6226734478), (1707.1488854866, 3143.6226734478), (4145.294248573335, 5583.434606202038), (5824.079452685, 7443.9945330296), (11739.852743984931, 13729.246567556096), (9513.62385030349, 10937.820957812448), (30892.28005012768, 32313.662880091983)]
答案 3 :(得分:0)
一个。您可以像这样迭代a
元素,但这会自行更改元素
for ind in range(len(a)):
a[ind][0] += b[0]
a[ind][1] += b[1]
B中。您可以使用列表推导:
c = [[item[0] + b[0], item[1] + b[1]] for item in a]
℃。您可以使用map
:
c = map(lambda x: [x[0] + b[0], x[1] + b[1]], a)
答案 4 :(得分:0)
使用numpy
>>>import numpy as np
>>>a =np.array( [[4251, 4254], [4123, 4290], [4323, 4222], [4111, 4900]])
>>>b =np.array([1324, 2745])
>>>a+b
array([[5575, 6999],
[5447, 7035],
[5647, 6967],
[5435, 7645]])
答案 5 :(得分:0)
`
>>> a = [[4251, 4254], [4123, 4290], [4323, 4222], [4111, 4900]] #original array
>>> b = [1324, 2745] #original array (note length of b and of individual lists of a must be same)
>>> c=[] #temporary list
>>> final_list=[]
>>> for val_a in a: #for each element(list) in a, compute c
c=[] #initialize temp list every time
for index in range(len(b)): #for each element in b, compute c (note length issue mentioned above)
c.append(val_a[index]+b[index]) #add the values and append to c
final_list.append(c) #append c to final list
>>> final_list
[[5575, 6999], [5447, 7035], [5647, 6967], [5435, 7645]]
`
很高兴帮助