<p>我做这种手术有问题,比如说我们有一根绳子</p>
<pre><code>teststring = "This is a test of number, number: 525, number: 585, number2: 559"
</code></pre>
<p>我想把525和585存储到一个列表中,怎么做?</p>
<p>我用了一种非常愚蠢的方法,但肯定有更好的方法</p>
<pre><code>teststring = teststring.split()
found = False
for word in teststring:
if found:
templist.<a href="https://www.cnpython.com/list/append" class="inner-link">append</a>(word)
found = False
if word is "number:":
found = True
</code></pre>
<p>有regex的解决方案吗?</p>
<p>如果我想储存525585和559呢?</p>
<p>你可以试试这个:</p>
<pre><code>import re
[int(x) for x in re.findall(r' \d+', teststring)]
</code></pre>
<p>它会给你:</p>
<pre><code>[525, 585, 559]
</code></pre>