Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>The Python documentation includes a section on <a href="http://docs.python.org/library/re.html#simulating-scanf" rel="nofollow">simulating <code>scanf</code></a>, which gives you some idea of how you can use regular expressions to simulate the behavior of <code>scanf</code> (or <code>sscanf</code>, it's all the same in Python). In particular, <code>r'\-?\d+'</code> is the Python string that corresponds to the regular expression for an integer. (<code>r'\d+'</code> for a nonnegative integer.) So you could embed this in your loop as</p> <pre><code>integer = re.compile(r'\-?\d+') for p in expression: if p.isdigit(): # somehow find the current position in the string integer.match(expression, curpos) </code></pre> <p>But that still reflects a very C-like way of thinking. In Python, your iterator variable <code>p</code> is really just an individual character that has actually been pulled out of the original string and is standing on its own. So in the loop, you don't naturally have access to the current position within the string, and trying to calculate it is going to be less than optimal.</p> <p>What I'd suggest instead is using Python's built in regexp matching iteration method:</p> <pre><code>integer = re.compile(r'\-?\d+') # only do this once in your program all_the_numbers = integer.findall(expression) </code></pre> <p>and now <code>all_the_numbers</code> is a list of string representations of all the integers in the expression. If you wanted to actually convert them to integers, then you could do this instead of the last line:</p> <pre><code>all_the_numbers = [int(s) for s in integer.finditer(expression)] </code></pre> <p>Here I've used <code>finditer</code> instead of <code>findall</code> because you don't have to make a list of all the strings before iterating over them again to convert them to integers.</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.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      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