简体   繁体   English

Python:嵌套在for循环中的if语句中的索引号

[英]Python: Acessing index numbers within an if statement nested within a for loop

I have an array of integers derived from a gradient of a line. 我有一个从一条线的渐变派生的整数数组。 The array is called sign_slope and looks like this: 该数组称为sign_slope,如下所示:

sign_slope = array([-1, 1, -1, ..., -1, -1, -1])

I am searching for the cases whereby consecutive items within the array are: 1, -1 For example you can see from the above output of sign_slope that: sign_slope[1] = 1 and sign_slope[2] = -1 This would be the first case of many that I would like to detect item/index numbers for. 我正在搜索数组中连续项目的情况:1,-1例如,您可以从sign_slope的上述输出中看到:sign_slope [1] = 1和sign_slope [2] = -1这将是第一个很多我希望检测项目/索引号的情况。 I would like the code to output an array or list of index numbers corresponding to the (n-1)'th index ie sign_slope[1] in the above case. 我希望代码输出一个数组或索引号列表,对应于第(n-1)个索引,即上述情况中的sign_slope [1]。 I have written the below printing statement which appears to work. 我写了下面的印刷声明似乎有效。 However, I do not know how to output the index numbers rather than the values as currently is the case and append them to a list or input them into an array. 但是,我不知道如何输出索引号而不是当前的值,并将它们附加到列表或将它们输入到数组中。

for n in range(0, len(sign_slope)):
    if sign_slope[n] < 0 and sign_slope[n - 1] > 0:
        print sign_slope[n - 1]
    else:
        print 0

Thanks is advance, 谢谢你的进步,

Kane 凯恩

Looping over a range of indicies is generally considered very unpythonic. 一系列的指标通常被认为是非常不合理的。 It reads very poorly and masks what you are really trying to do. 它读得很差,掩盖了你真正想做的事情。 As such, a better solution to finding your sublist is to loop through using the enumerate() builtin to get the indicies alongside the values. 因此,找到子列表的更好解决方案是使用enumerate()内置循环来获取值和值。 We can also provide a much more generic solution, and make it a generator which makes it easy to use. 我们还可以提供更通用的解决方案,并使其成为易于使用的生成器。

def find_sublists(seq, sublist):
    length = len(sublist)
    for index, value in enumerate(seq):
        if value == sublist[0] and seq[index:index+length] == sublist:
            yield index

What we do here is loop through the list, taking the indicies where the value matches the beginning of our sublist. 我们在这里做的是循环遍历列表,获取值与子列表的开头匹配的指标。 We then check to see if that segment of the list matches our sublist, if it does, we yield the index. 然后我们检查列表的那个段是否与我们的子列表匹配,如果是,我们yield索引。 This allows us to quickly find all matching sublists in a list. 这允许我们快速查找列表中的所有匹配的子列表。

We can then use this like so, using the list builtin to create a list from the generator: 然后,我们可以用这个像这样,使用list内建创造了发电机列表:

>>> list(find_sublists([-1, 1, -1, -1, -1, 1, -1], [1, -1]))
[1, 5]

You can do this without writing a loop, assuming your array sign_slope is a numpy array: 假设您的数组sign_slope是一个numpy数组,您可以在不编写循环的情况下执行此操作:

import numpy as np

# some data
sign_slope = np.array([-1, 1, -1, 1, 1, -1, -1])

# compute the differences in s
d = np.diff(sign_slope)

# compute the indices of elements in d that are nonzero
pos = np.where(d != 0)[0]

# elements pos[i] and pos[i]+1 are the indices
# of s that differ:
print s[pos[i]], s[pos[i]+1]

Here's an ipython session showing the values of the variables: 这是一个显示变量值的ipython会话:

In [1]: import numpy as np

In [2]: s = np.array([-1, 1, -1, 1, 1, -1, -1])

In [3]: d = np.diff(s)

In [4]: print d
[ 2 -2  2  0 -2  0]

In [5]: pos = np.where(d != 0)[0]

In [6]: print pos
[0 1 2 4]

In [7]: print s[pos[0]], s[pos[0]+1]
-1 1

In [8]: print s[pos[1]], s[pos[1]+1]
1 -1

In [9]: print s[pos[2]], s[pos[2]+1]
-1 1

In [10]: print s[pos[3]], s[pos[3]+1]
1 -1

Hope this helps 希望这可以帮助

Edit: Actually, in retrospect, I'm missing some of the differences. 编辑:实际上,回想起来,我错过了一些差异。 I'll get back to you on that. 我会回复你的。 Sorry for any confusion. 对不起任何困惑。

Edit 2: Fixed, I made a silly error. 编辑2:固定,我犯了一个愚蠢的错误。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM