擅长:python、mysql、java
<p>如果你想写</p>
<pre><code>$ python life.py 0101010,0101010,0101010
</code></pre>
<p>然后需要来自<code>sys</code>模块的<code>argv</code>向量,它包含命令行中的所有元素,然后是一个简单的列表理解
将第一个参数拆分为逗号,从而得到矩阵的<code>r</code>ows(注意这些行是字符串),然后构建一个子列表,转换一行中的每个字符</p>
^{pr2}$
<p>为了详细说明这个过程,使用一个辅助变量来代替<code>argv[1]</code></p>
<pre><code>In [46]: m = '0101010,0101010,0101010'
In [47]: [r for r in m.split(',')]
Out[47]: ['0101010', '0101010', '0101010']
In [48]: [[c for c in r] for r in m.split(',')]
Out[48]:
[['0', '1', '0', '1', '0', '1', '0'],
['0', '1', '0', '1', '0', '1', '0'],
['0', '1', '0', '1', '0', '1', '0']]
In [49]: [[int(c) for c in r] for r in m.split(',')]
Out[49]: [[0, 1, 0, 1, 0, 1, 0], [0, 1, 0, 1, 0, 1, 0], [0, 1, 0, 1, 0, 1, 0]]
In [50]: np.array([[int(c) for c in r] for r in m.split(',')])
Out[50]:
array([[0, 1, 0, 1, 0, 1, 0],
[0, 1, 0, 1, 0, 1, 0],
[0, 1, 0, 1, 0, 1, 0]])
In [51]:
</code></pre>