본문 바로가기

내가 당면한 문제와 해결방안

python for iterate loop check next item

https://stackoverflow.com/questions/1011938/python-previous-and-next-values-inside-a-loop

 

Python - Previous and next values inside a loop

How can I do thing like this in python? foo = somevalue previous = next = 0 for (i=1; i<objects.length(); i++)="" {="" if="" (objects[i]="=foo){" previous="objects[i-1]" next="object...</p"> </objects.length();>

stackoverflow.com

 

This should do the trick.

foo = somevalue
previous = next_ = None
l = len(objects)
for index, obj in enumerate(objects):
    if obj == foo:
        if index > 0:
            previous = objects[index - 1]
        if index < (l - 1):
            next_ = objects[index + 1]
Here's the docs on the enumerate function.

enumerate객체로 만들긔

'내가 당면한 문제와 해결방안' 카테고리의 다른 글

couchbase  (0) 2019.08.29
spring  (0) 2019.08.17
crawling wikipedia  (0) 2019.07.31
RestController랑 그냥 Controller  (0) 2019.07.26
pageable  (0) 2019.07.25