1

I'm studying Python and I don't understand how to use iterators.

I need to write code. In C it'll be like this:

list_node *cp = list_of_chars;
char dp = '>'; int flag = 0;
while (cp != NULL)
{ if( isdigit(cp->val) ) 
  { printf("%c",cp->val);
    if( cp->val == '0' )
    { cp->prev->next = cp->next; cp->next->prev = cp->prev; }
    else cp->val--;
  }
  else if( (cp->val == '>') || (cp->val == '<') )
  { dp = cp->val; flag = 1; }

       if( dp == '>' ) cp = cp->next;
  else if( dp == '<' ) cp = cp->prev;
  else return ERR;

  if( flag && ( (cp->val == '>') || (cp->val == '<') ))
  { cp->prev->prev->next = cp;
    cp->prev = cp->prev->prev;
  }
}

Can you help me translate this code to python? I started to write, but have some errors and I not sure, that I understand documentation.

  ip = {'cp' : iter(program), 'dp' : '>'}
  flag = 0

  while ip['cp'] != []:

    if ('0' <= ip['cp']) & (ip['cp'] <= '9'):
      print ip['cp']
      if ip['cp'] == '0': ip['cp']  = []
      else:               ip['cp'] -= 1

    elif (ip['cp'] == '>') | (ip['cp'] == '<'):
      ip['dp'] = ip.['cp']
      flag = 1

    else: raise NameError('incorrect cp-value')

    if   ip['dp'] == '>': ip['cp'].next()
    elif ip['dp'] == '<': ip['cp'].prev()
    else: raise NameError('incorrect dp-value')

    if flag & ( (ip['cp'] == '>') | (ip['cp'] == '<') ):
      ip['cp'].prev()
      ip['cp'] = []

The question is how can I get the value of the iterator without the function next().

Examples of python-expert code with advanced uses of iterators would also be nice to see.

Sam Mussmann
  • 5,883
  • 2
  • 29
  • 43
Kirill Golikov
  • 1,354
  • 1
  • 13
  • 27
  • 10
    Trying to directly translate C to Python is a terrible idea. You need to translate the concept, not the code. Python is a higher level language and doing things the way one would in C, while in Python, will produce horrible, unpythonic code. – Gareth Latty Nov 10 '12 at 16:54
  • I know C-prog.lang. And think on it. I try to explain what I need to you and use C for it. I can't explain algorithm another way. – Kirill Golikov Nov 10 '12 at 16:59
  • 1
    @so-olitary Then your issue is you can't define the question well. You need to give us an exact question. Here you are saying *it doesn't work* - that isn't specific enough. What is the error you get? What is the problem? – Gareth Latty Nov 10 '12 at 17:06
  • It seems NULL checks are missing in your C code (when you're removing nodes from the list, advance the pointer). Could you provide [an example input such as `234<5>6` and the expected output e.g., `234321`](http://ideone.com/CpCZN8). Is it correct that `flag` is set only once?Shouldn't it be set to zero at some point inside the loop? – jfs Nov 10 '12 at 18:38
  • @J.F. Sebastian Yes, there are all my faults. But I didn't write code for compiler. I tried to explain algorithm. Thank you for your corrections and your example of python-code. – Kirill Golikov Nov 10 '12 at 18:47

2 Answers2

3

Okay here are lots of problems in you python code.

Well start with the simple

while ip['cp'] != []:

Could be written to simply

while ip['cp']:

It will do while ip['cp'] isn't a falsly value. [], None, '' are a falsly value for example.

Instead of:

if   ip['dp'] == '>': ip['cp'].next()
elif ip['dp'] == '<': ip['cp'].prev()
else: raise NameError('incorrect dp-value')

write

if ip['dp'] == '>': 
    ip['cp'].next()
elif ip['dp'] == '<':
    ip['cp'].prev()
else: 
    raise NameError('incorrect dp-value')

Even if in some case, writing inline code can work. Think about readability. Inline code is almost never a good start to make code readable and easy to debug. It will also make 90% of your syntax error easy to fix.

Here's where the fun starts...

  if( cp->val == '0' )
  { cp->prev->next = cp->next; cp->next->prev = cp->prev; }
  else cp->val--;

and

  if ip['cp'] == '0': ip['cp']  = []
  else:               ip['cp'] -= 1

In python '0' is the string 0. In python you're working with string when you have a string object. You're working with numbers when you have numbers... In other words the code you're trying to write in python can't be purely translated to python. The last char in a python string is not '\0'. It's the last char in the string. In other words, you can really test for the last char like in C.

Now lets talk about iterators

That said, disregarding all other problems in your code, here is how to work with iterators.

iter() is a builtin function that returns an iterator. In most case you don't have to call it yourself. There are constructions that will do that for you.

for example, you can write :

iterable = iter('string')
iterable.next() == 's'
iterable.next() == 't'
... Until StopIteration is raised

for i in iter('string'):

Is the same as:

for i in 'string':

That said, iterators are used to iter over something. What you are really looking for isn't an iterator. Because iterator are one way thing. You can go to the end and you can't go back. There is no previous. Your C code implement a linked-list that can be used with an iterable but in your code, you're changing the position of some nodes.

If you want to write python code, don't write C code in python

Loïc Faure-Lacroix
  • 13,220
  • 6
  • 67
  • 99
2

Iterators in Python can only go forward. The only operation supported on them is next(), and that either returns the next value, or raise a StopIteration exception. They aren't suitable for this problem, where you need to move forward and back.

Your original code is confusing, but I would use a list and an integer index to solve this in Python.

Ned Batchelder
  • 364,293
  • 75
  • 561
  • 662