擅长:python、mysql、java
<p>如果模式没有如评论中所要求的那样发生剧烈变化,则有三个步骤:</p>
<ol>
<li>通过正则表达式查找每个列表的内容</li>
<li>在<code>,</code>上拆分内容</li>
<li>将第二个元素转换为数字</li>
</ol>
<pre class="lang-py prettyprint-override"><code>import re
s = "[abc,7.56],[dfe,2.44],[iij,2.31]"
list_contents = re.findall(r"\[(.+?)\]", s) # non-greedily take what are between [..]
split_as_strings = [content.split(",") for content in list_contents]
result = [[pair[0], float(pair[1])] for pair in split_as_strings]
</code></pre>
<p><code>result</code>将是</p>
<pre><code>[['abc', 7.56], ['dfe', 2.44], ['iij', 2.31]]
</code></pre>
<p>和中间结果:</p>
<pre><code>list_contents == ['abc,7.56', 'dfe,2.44', 'iij,2.31']
split_as_strings == [['abc', '7.56'], ['dfe', '2.44'], ['iij', '2.31']]
</code></pre>