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.