从 Python 中不同列表中的第 n 项减去列表中的第 n 项

subtract nth item from list from nth item in different list in Python

我要修改这段代码:

def differenceinX(list1,list2):
    answer=[n1 - n2 for (n1, n2) in zip(list1, list2)]
    return answer

类似于:

def differenceinX[x](list1[x],list2[x]):
    answer=[n1 - n2 for (n1, n2) in zip(list1[x], list2[x])]
    return answer

我有 2 个列表 (23,24,26), (24,24,25),我希望能够从第二个列表的第一项中减去第一个列表中的第一项。我收到错误消息 'invalid syntax'

如果您只是将输出作为一个列表项的单个数字来查找,请使用此方法:

def differenceinX(list1, list2, x):
    return list1[x] - list2[x]

我认为这会满足您的需求,

def differenceinX(list1,list2, index):
  answer = list1[index] - list2[index]
  return answer