最好的方法是什么?希望采取差异,但不喜欢这种可怕的方式。对于每个A,B,C,它从subtract from
A = [500, 500, 500, 500, 5000]
B = [100, 100, 540, 550, 1200]
C = [540, 300, 300, 100, 10]
triples= [tuple(A),tuple(B), tuple(C)]
subtract_from = tuple([1234,4321,1234,4321,5555])
diff = []
for main in subtract_from:
for i in range(len(triples)):
for t in triples[i]:
diff[i].append(main-t)
答案 0 :(得分:1)
尝试这样的事情:
const path = require('path');
const cp = require('child_process');
const myPath = path.join(__dirname, '..', 'quake1', 'fteqwsv64.exe');
//Launch the Quake1 server, direct the output to node's process output. Don't take input.
let quake = cp.spawn(myPath, {
stdio: ['ignore', 'inherit', 'inherit']
});
这是做到这一点的最佳做法。无需导入任何库,只需使用builtins。
答案 1 :(得分:0)
您可以尝试使用地图和操作符:
import operator
A = [500, 500, 500, 500, 5000]
B = [100, 100, 540, 550, 1200]
C = [540, 300, 300, 100, 10]
l = [A, B, C]
subtract_from = [1234,4321,1234,4321,5555]
diff = list((list(map(operator.sub, subtract_from , i)) for i in l))
print(diff)
# [[734, 3821, 734, 3821, 555], [1134, 4221, 694, 3771, 4355], [694, 4021, 934, 4221, 5545]]
答案 2 :(得分:0)
首先,如果你想要元组,请明确使用元组而不转换列表。话虽这么说,你应该写这样的东西:
a = 500, 500, 500, 500, 5000
b = 100, 100, 540, 550, 1200
c = 540, 300, 300, 100, 10
vectors = a, b, c
data = 1234, 4321, 1234, 4321, 5555
diff = [
[de - ve for de, ve in zip(data, vec)]
for vec in vectors
]
如果您需要元组列表,请使用tuple(de - ve for de, ve in zip(data, vec))
代替[de - ve for de, ve in zip(data, vec)]
。
答案 3 :(得分:0)
我认为其他所有人都已经用列表推导来指出它所以这里有几个奇怪的例子,如果你使用一个可变列表并以命令式样式重用它是可接受的样式,那么下面的代码可以完成
A = [500, 500, 500, 500, 5000]
B = [100, 100, 540, 550, 1200]
C = [540, 300, 300, 100, 10]
subtract_from = (1234,4321,1234,4321,5555)
for i,x in enumerate(subtract_from):
A[i], B[i], C[i] = x-A[i], x-B[i], x-C[i]
# also with map
#for i,x in enumerate(zip(subtract_from,A,B,C)):
# A[i], B[i], C[i] = map(x[0].__sub__, x[1:])
diff = [A,B,C]
它不那么优雅但效率更高*(......我没有为此声明做过任何基准测试)