简体   繁体   English

带有下一行值的Python循环

[英]Python loop with next row values

Is there a way to make my loop work with no errors because there is no next value? 有没有一种方法可以使我的循环正常工作,因为没有下一个值? Or not to use a foor loop for this at all? 还是根本不使用foor循环?

Inside this function below I have another function with a for loop: 在下面的此函数内,我还有一个带for循环的函数:

def funcA(self,perc,bloc):
    def funcA1(self):
        maxIndex = len(self)
        localiz = self.loc
        for x in range(0,maxIndex-1):
            if localiz[x,bloc] == localiz[x+1,bloc]:
                localiz[x,"CALC"] = True
            else:
                localiz[x,"CALC"]= False
        return self

I got it working by creating first the column "CALC" with False because the last line of my df will always be False . 我首先通过使用False创建列“ CALC”使其工作,因为df的最后一行始终为False But surely there is a better way. 但是肯定有更好的方法。

EDIT I'm basically using pandas and numpy for this code. 编辑我基本上是使用pandas和numpy作为此代码。

The bloc that i'm using in the function is the ID column The data structure I'm working with is like this: 我使用在功能上是集团ID列中的数据结构我工作是这样的:

ID   NUMBER
2    100
2    150
3    500
4    100
4    200
4    250

And the expected results are: 预期结果是:

ID   NUMBER   CALC
2    100      True
2    150      False
3    500      False
4    100      True
4    200      True
4    250      False

a pythonic way is this: 一个pythonic方式是这样的:

lst = [char for char in 'abcdef']
print(lst)
for i, (cur, nxt) in enumerate(zip(lst, lst[1:])):
    print(i, cur, nxt)

just note that cur will only run to the second-to-last element of lst . 请注意, cur仅会运行到lst的倒数第二个元素。

this will print: 这将打印:

['a', 'b', 'c', 'd', 'e', 'f']
0 a b
1 b c
2 c d
3 d e
4 e f

i is the index in lst of the cur element. i是在索引lst所述的cur元件。

lst[1:] creates a new list excluding the first element. lst[1:]创建一个不包含第一个元素的新列表。 if your lists are very long you may consider replaicing that part with islice ; 如果您的清单很长,您可以考虑用islice替换该部分; that way no additional copy is made. 这样就不会产生其他副本。


this also works if your arr is an n-dimensional numpy array: 如果您的arr是n维numpy数组,这也可以使用:

import numpy as np

arr = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]], np.int32)
print(arr)
for i, (cur, nxt) in enumerate(zip(arr, arr[1:])):
    print(i, cur, nxt)

with ouput: 输出:

[[1 2 3]
 [4 5 6]
 [7 8 9]]
0 [1 2 3] [4 5 6]
1 [4 5 6] [7 8 9]

Because I'm not familiar with this vector-style solution that numpy gives us, I think I couldn't make the most of the proposed solution that was given. 因为我不熟悉numpy为我们提供的矢量风格解决方案,所以我想我无法充分利用给出的建议解决方案。

I did find a way to overcome the loop I was using though: 我确实找到了一种方法来克服我正在使用的循环:

def funcA(self,perc,bloc):
    def new_funcA1(self):

        df = self[[bloc]]
        self['shift'] = df.shift(-1)
        self['CALC'] = self[bloc] == self['shift']
        self.drop('shift', axis=1, inplace=True)
        return self

With pandas.DataFrame.shift(-1) the last row will return NaN. 使用pandas.DataFrame.shift(-1),最后一行将返回NaN。 This way I don't have to make any adjustments for the first or last row and I got rid of the loop! 这样,我不必对第一行或最后一行进行任何调整,并且摆脱了循环!

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

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