Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to use iterators in python
    primarykey
    data
    text
    <p>I'm studying Python and I don't understand how to use iterators.</p> <p>I need to write code. In C it'll be like this:</p> <pre><code>list_node *cp = list_of_chars; char dp = '&gt;'; int flag = 0; while (cp != NULL) { if( isdigit(cp-&gt;val) ) { printf("%c",cp-&gt;val); if( cp-&gt;val == '0' ) { cp-&gt;prev-&gt;next = cp-&gt;next; cp-&gt;next-&gt;prev = cp-&gt;prev; } else cp-&gt;val--; } else if( (cp-&gt;val == '&gt;') || (cp-&gt;val == '&lt;') ) { dp = cp-&gt;val; flag = 1; } if( dp == '&gt;' ) cp = cp-&gt;next; else if( dp == '&lt;' ) cp = cp-&gt;prev; else return ERR; if( flag &amp;&amp; ( (cp-&gt;val == '&gt;') || (cp-&gt;val == '&lt;') )) { cp-&gt;prev-&gt;prev-&gt;next = cp; cp-&gt;prev = cp-&gt;prev-&gt;prev; } } </code></pre> <p>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.</p> <pre><code> ip = {'cp' : iter(program), 'dp' : '&gt;'} flag = 0 while ip['cp'] != []: if ('0' &lt;= ip['cp']) &amp; (ip['cp'] &lt;= '9'): print ip['cp'] if ip['cp'] == '0': ip['cp'] = [] else: ip['cp'] -= 1 elif (ip['cp'] == '&gt;') | (ip['cp'] == '&lt;'): ip['dp'] = ip.['cp'] flag = 1 else: raise NameError('incorrect cp-value') if ip['dp'] == '&gt;': ip['cp'].next() elif ip['dp'] == '&lt;': ip['cp'].prev() else: raise NameError('incorrect dp-value') if flag &amp; ( (ip['cp'] == '&gt;') | (ip['cp'] == '&lt;') ): ip['cp'].prev() ip['cp'] = [] </code></pre> <p>The question is how can I get the value of the iterator without the function next().</p> <p>Examples of python-expert code with advanced uses of iterators would also be nice to see.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload