回答此问题可获得 20 贡献值,回答如果被采纳可获得 50 分。
<p>我正在使用某人的Java代码,其中关键数据结构是<code>m x n x p</code>数组<code>float[][][]</code>。我需要把它放到Python中;目前我的方法是使用数组.deepToString然后从Python解析文本文件。你知道吗</p>
<p>我被困在如何写一个正则表达式来解析txt。
我能做的是用科学记数法找出所有的浮点数及其相关的指数。
我使用以下模式来执行此操作:</p>
<pre><code>float_pat = r'\d\.\d*(?:E-\d+)?'
</code></pre>
<p>这可以很好地用科学记数法捕获由deepToString输出的浮点数。注意这些值都是正的,因为它们是概率。也就是说,我对如何捕捉数字本身没有任何问题。你知道吗</p>
<p>我不能做的是让regex搜索左括号和右括号中的任意数量的浮点数。我试过这个:</p>
<pre><code>list_of_floats_pat = r'\[(?:\d\.\d*(?:E-\d+)?), )+\]'
</code></pre>
<p>在这里,我试图找到一个或多个浮点格式,后跟逗号和方括号括起的空格。但这会返回<code>[]</code>。不知道我不明白什么。你知道吗</p>
<p>下面是一个2x1x1阵列示例:</p>
<pre><code>[[[0.6453525160688715, 0.15620941152962334, 0.1874313118193626, 9.991008092716556E-5, 9.991008092716556E-5, 9.991008092716556E-5, 9.991008092716556E-5, 0.01050721017750691, 9.991008092716556E-5], [0.5904776610141782, 0.18175460267577365, 9.991008092716556E-5, 0.22716827582448523, 9.991008092716556E-5, 9.991008092716556E-5, 9.991008092716556E-5, 9.991008092716556E-5, 9.991008092716556E-5]]]
</code></pre>
<p>我希望正则表达式返回两个匹配项:</p>
<pre><code>0.6453525160688715, 0.15620941152962334, 0.1874313118193626, 9.991008092716556E-5, 9.991008092716556E-5, 9.991008092716556E-5, 9.991008092716556E-5, 0.01050721017750691, 9.991008092716556E-5
</code></pre>
<p>以及</p>
<pre><code>0.5904776610141782, 0.18175460267577365, 9.991008092716556E-5, 0.22716827582448523, 9.991008092716556E-5, 9.991008092716556E-5, 9.991008092716556E-5, 9.991008092716556E-5, 9.991008092716556E-5
</code></pre>
<p>然后我就可以用strip和split解析为字符串。你知道吗</p>
<p>我找到了一个解决方法,在这里我可以找到所有的括号索引。但我想知道我对正则表达式有什么不了解。你知道吗</p>