it_*_*ure 1 python scalar numpy
>>> numpy.sin(range(11))
array([ 0. , 0.84147098, 0.90929743, 0.14112001, -0.7568025
-0.95892427, -0.2794155 , 0.6569866 , 0.98935825, 0.4121184
-0.54402111])
>>> numpy.array(range(11))*2
array([ 0, 2, 4, 6, 8, 10, 12, 14, 16, 18, 20])
>>> str(numpy.array(range(11))... )
'[ 0 1 2 3 4 5 6 7 8 9 10]'
Run Code Online (Sandbox Code Playgroud)
我怎样才能得到 ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '10'] 与numpy.sin(range(11)) 或 numpy.array(range(11))*2 等 python 标量的概念?
我可以通过以下方式做到这一点:
>>> s=[]
>>> [s.append(str(i)) for i in range(11)]
>>> s
['0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '10']
>>> str(numpy.array(range(11)))
'[ 0 1 2 3 4 5 6 7 8 9 10]'
Run Code Online (Sandbox Code Playgroud)
我想要的是 ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '10'] ,不是 '[ 0 1 2 3 4 5 6 7 8 9 10]' 。
它以list comprehension的概念获取字符串数组,如何通过概念--python scalars来完成工作?
我认为这就是你想要的:
>>> a = numpy.arange(10)
>>> a
array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
>>> a.astype(str)
array(['0', '1', '2', '3', '4', '5', '6', '7', '8', '9'],
dtype='|S21')
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
149 次 |
| 最近记录: |