Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>It's not entirely obvious from your code, but it looks like each line can have one "hit" at most; if that's indeed the case, then something like:</p> <pre><code>import re def parse_params(num_of_params, lines): sn = 'Names Params Optimize Upper Lower'.split() ks = '''paramName paramValue optimizeParam paramLowerBound paramUpperBound'''.split() vals = dict((k, []) for k in ks) are = re.compile(r'model\.(%s) (\d+) (.*)' % '|'.join(ks)) for line in lines: mo = are.search(line) if not mo: continue p = int(mo.group(2)) if p &lt; 1 or p &gt; num_of_params: continue vals[mo.group(1)].append(mo.group(3).rstrip()) for k, s in zip(ks, sn): print '%-8s =' % s, print vals[k] </code></pre> <p>might work -- I exercised it with a little code as follows:</p> <pre><code>if __name__ == '__main__': lines = '''model.paramUpperBound 1 ZAP model.paramLowerBound 1 zap model.paramUpperBound 5 nope'''.splitlines() parse_params(2, lines) </code></pre> <p>and it emits</p> <pre><code>Names = [] Params = [] Optimize = [] Upper = ['zap'] Lower = ['ZAP'] </code></pre> <p>which I think is what you want (if some details must differ, please indicate exactly what they are and let's see if we can fix it).</p> <p>The two key ideas are: use a <code>dict</code> instead of lots of <code>if</code>s; use a <code>re</code> to match "any of the following possibilities" with parenthesized groups in the re's pattern to catch the bits of interest (the keyword after <code>model.</code>, the integer number after that, and the "value" which is the rest of the line) instead of lots of <code>if x in y</code> checks and string manipulation.</p>
 

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