[英]How to subtract corresponding element of a specific slice in two long lists in Python?
suppose I have following two lists: 假设我有以下两个列表:
x = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15]
y = [None,None,None,None,None,10,20,30,40,50,60,70,80,90,100]
I need to subtract the corresponding elements of each list (x[i] - y[i] ) and I want to start subtracting from 6th element of x which is 6, so that the corresponding element in y is not null(None). 我需要减去每个列表的相应元素(x [i] - y [i]),我想开始从x的第6个元素中减去6,这样y中的对应元素就不是null(None)。 the following code is what I have tried and got error: 以下代码是我尝试过并得到的错误:
result = []
for i in x[5:]:
result.append(x[i] - y[i])
Index Error: list index out of range 索引错误:列表索引超出范围
You should do it like this: 你应该这样做:
for val1, val2 in zip(x[5:], y[5:]):
result.append(val1 - val2)
OR 要么
for val1, val2 in list(zip(x, y))[5:]:
result.append(val1 - val2)
You could also just skip over the None
values, like this: 您也可以跳过None
值,如下所示:
for val1, val2 in zip(x, y):
if val2 is not None: # can also check if val1 is not None if needed
result.append(val1 - val2)
The reason you're getting the IndexError
is that the i
in your loop is getting assigned the values (not indeces!) of the x
list, and you're trying to index the lists with those values. 你得到IndexError
的原因是你的循环中的i
被分配了x
列表的值(而不是indeces!),并且你试图用这些值索引列表。 So for example on the last go of the loop i = 15
when the index of that element is only 14
. 因此,例如在循环的最后一次,当该元素的索引仅为14
时, i = 15
。
You get an error, because you're trying to access y[15]
(since 15 is the value of x[14]
). 你得到一个错误,因为你试图访问y[15]
(因为15是x[14]
的值)。
But y
only has 15 elements, and since list items start at index 0, you get list index out of range
. 但是y
只有15个元素,并且由于列表项从索引0开始,因此list index out of range
。
A more flexible approach - as long as your both lists have the same length, is to use zip
and skip the None
values: 更灵活的方法 - 只要您的两个列表具有相同的长度,就是使用zip
并跳过None
值:
x = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15]
y = [None,None,None,None,None,10,20,30,40,50,60,70,80,90,100]
result = []
for val_x, val_y in zip(x, y):
if(val_y is None):
continue
result.append(val_x - val_y)
print(result)
Output: 输出:
[-4, -13, -22, -31, -40, -49, -58, -67, -76, -85]
Or as a list comprehension: 或者作为列表理解:
result = [ (val_x - val_y) for val_x, val_y in zip(x, y) if (val_y is not None) ]
You can use enumerate
您可以使用enumerate
Ex: 例如:
x = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15]
y = [None,None,None,None,None,10,20,30,40,50,60,70,80,90,100]
result = []
for i, v in enumerate(x[5:], 5):
result.append(v - y[i])
print(result)
Output: 输出:
[-4, -13, -22, -31, -40, -49, -58, -67, -76, -85]
As @meowgoesthedog said in his comment, array indices start from 0. 正如@meowgoesthedog在评论中所说,数组索引从0开始。
Also, as your 2 list are of same size, you can zip them, to make the code look nicer, so here's a solution with zipped lists : 此外,由于你的2个列表大小相同,你可以压缩它们,使代码看起来更好,所以这里有一个带有压缩列表的解决方案:
result = []
for i, j in list(zip(x, y))[5:]:
result.append(i - j)
And here's a solution without : 这是一个没有的解决方案:
result = []
for i in range(x[5:] - 1):
result.append(x[i] - x[j])
Try this, it will take care of "None"s in both the lists. 试试这个,它将在两个列表中处理“无”。 We need not track them manually. 我们无需手动跟踪它们。 If we don't need first 5 then we can just add a slice on subtract. 如果我们不需要前5,那么我们可以在减法上添加切片。
subtrct=[None if val is None else (val - (0 if y[index] is None else y[index])) for index, val in enumerate(x)]
You can use the functions starmap()
and dropwhile()
: 您可以使用函数starmap()
和dropwhile()
:
from itertools import starmap, dropwhile
x = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15]
y = [None, None, None, None, None, 10, 20, 30, 40, 50, 60, 70, 80, 90, 100]
# substract corresponding elements
m = starmap(lambda x, y: x - y if y else None, zip(x, y))
# filter None
list(dropwhile(lambda x: not x, m))
# [-4, -13, -22, -31, -40, -49, -58, -67, -76, -85]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.