manipulation of a list of strings containing digits to output a list of of digits
<p>I looking for help in manipulating a list of strings where I want to extract the digits such has :</p>

<pre><code> x = ['aa bb qq 2 months  60%', 'aa bb qq 3 months  70%', 'aa bb qq 1 month  80%']
</code></pre>

<p>I am trying to get to :</p>

<pre><code>[[2.0,60.0],[3.0,70.0],[1.0,80.0]]
</code></pre>

<p>in a elegant fashion.</p>

<p>The first number should always be an integer but the second number can be a float with a decimal value</p>

<p>my dirty work around is this:</p>

<pre><code>x_split = [y.replace("%", "").split() for y in x]
x_float = [[float(s) for s in x if s.isdigit()] for x in x_split]

Out[100]: [[2.0, 60.0], [3.0, 70.0], [1.0, 80.0]]
</code></pre>

<p>Use a <a href="https://docs.python.org/2/library/re.html" rel="nofollow">regular expression</a> to match integers and floats.</p>

<pre><code>import re
[[float(n) for n in re.findall(r'\d+\.?\d*', s)] for s in x]
</code></pre>

<p>Explanation for the regex (<code>r'\d+\.?\d*'</code>):  </p>

<pre><code>r    #  a raw string so that back slashes are not converted  
\d   #  digit 0 to 9
+    #  one or more of the previous pattern (\d)
\.   #  a decimal point
?    #  zero or one of the previous pattern (\.)
\d   #  digit 0 to 9
*    #  zero or more of the previous pattern (\d)
</code></pre>

python string list python-2.7