2

I am quite new to python.

Roughly i want to do the following:

var=0
while x doesn't change
   return var

   if x changes
      var = var + 1

This shall then be continued until all x-values are processed. I don't have a list of the x-values.

Any suggestions how to get from my ideas to code?

I am working in field calculator in Arcmap.

ustroetz
  • 5,802
  • 16
  • 47
  • 74

2 Answers2

0
y = x
while x == y:
    # your code that might change x
ddinchev
  • 33,683
  • 28
  • 88
  • 133
0
def edgedetector(it):
    var = 0
    old_i = object() # guaranteed to be different from everything else
    for i in it:
        if i != old_i:
            var += 1
            old_i = i
        yield var

Having this generator function, you do the following:

Supposed you have an iterable it having the values you are interested in, you do

import itertools
it_a, it_b = itertools.tee(it) # split it up
for items, val in itertools.izip(it_a, edgedetector(it_b)):
    #do whatever you want to do with it

Alternative:

def edgedetector(it):
    var = 0
    old_i = object() # guaranteed to be different from everything else
    for i in it:
        if i != old_i:
            var += 1
            old_i = i
        yield i, var # this is changed...

makes it easier:

for items, val in edgedetector(it):
    #do whatever you want to do with it

object() is the instantiation of an instance of the class object. The generator function uses it to detect the inequality of the initial "old" object with the first element of the iterable.

glglgl
  • 89,107
  • 13
  • 149
  • 217
  • Thank you for the code! However I only get NULL values in return. The code works though. What could cause this? – ustroetz Oct 11 '12 at 14:53
  • Thanks for the very detailed explanation. Yet i only get NULL values as results. Any more ideas. I really don't know much about python. – ustroetz Oct 11 '12 at 15:08
  • @user1738154 Where exactly do you get the NULL values? (Or rather `None` values?) – glglgl Oct 11 '12 at 15:39