Python 是否可以创建一个NumPy字符数组,并在固定时间内访问该数组的连续切片? 这不是复制品
我搜索了很多,我很清楚很多相关主题的帖子已经存在了,但没有一个能回答这个简单问题 问题: 是否可以使用NumPy创建一个字符数组(假设使用unicode),然后允许在恒定时间内进行连续切片 期望行为 这里的总体复杂性应该是Python 是否可以创建一个NumPy字符数组,并在固定时间内访问该数组的连续切片? 这不是复制品,python,python-3.x,numpy,Python,Python 3.x,Numpy,我搜索了很多,我很清楚很多相关主题的帖子已经存在了,但没有一个能回答这个简单问题 问题: 是否可以使用NumPy创建一个字符数组(假设使用unicode),然后允许在恒定时间内进行连续切片 期望行为 这里的总体复杂性应该是O(n),而不是O(n**2) 实际行为 围绕着np_chars的实际行为是我对NumPy最意想不到的体验之一。我假设所有这些都是有充分理由的故意行为,如果有人能指出原因是什么,那就太好了 打印(np字符)打印xxxxxxxxx(好) len(np_chars)生成未大小对象的
O(n)
,而不是O(n**2)
实际行为
围绕着np_chars的实际行为是我对NumPy最意想不到的体验之一。我假设所有这些都是有充分理由的故意行为,如果有人能指出原因是什么,那就太好了
打印(np字符)
打印xxxxxxxxx
(好)
len(np_chars)
生成未大小对象的错误TypeError:len()
np\u字符大小为1(?)
np_字符形状
是()
(?)
np\u字符[0]
产生索引器:数组的索引太多(??)
np_chars.dtype
是dtype('
我看不出np\u chars
有什么奇怪的地方。您让它从一个字符串生成一个数组。它只是这样做了:
In [91]: np_chars
Out[91]: array('xxxxxxxxxx', dtype='<U10')
In [92]: np_chars.shape
Out[92]: ()
In [93]: np_chars[()]
Out[93]: 'xxxxxxxxxx'
从字符串获取子字符串:
In [96]: [chars[:i] for i in range(10)]
Out[96]:
['',
'x',
'xx',
'xxx',
'xxxx',
'xxxxx',
'xxxxxx',
'xxxxxxx',
'xxxxxxxx',
'xxxxxxxxx']
从字符数组获取数组切片:
In [97]: [_95[:i] for i in range(10)]
Out[97]:
[array([], dtype='<U1'),
array(['x'], dtype='<U1'),
array(['x', 'x'], dtype='<U1'),
array(['x', 'x', 'x'], dtype='<U1'),
array(['x', 'x', 'x', 'x'], dtype='<U1'),
array(['x', 'x', 'x', 'x', 'x'], dtype='<U1'),
array(['x', 'x', 'x', 'x', 'x', 'x'], dtype='<U1'),
array(['x', 'x', 'x', 'x', 'x', 'x', 'x'], dtype='<U1'),
array(['x', 'x', 'x', 'x', 'x', 'x', 'x', 'x'], dtype='<U1'),
array(['x', 'x', 'x', 'x', 'x', 'x', 'x', 'x', 'x'], dtype='<U1')]
但是这两种方法有很大的不同,因此比较它们没有多大意义。我们可以从数组切片生成字符串,但这需要更多的时间:
In [101]: [''.join(_95[:i]) for i in range(10)]
Out[101]:
['',
'x',
'xx',
'xxx',
'xxxx',
'xxxxx',
'xxxxxx',
'xxxxxxx',
'xxxxxxxx',
'xxxxxxxxx']
In [102]: timeit [''.join(_95[:i]) for i in range(10)]
41.5 µs ± 306 ns per loop (mean ± std. dev. of 7 runs, 10000 loops each)
即使我将n
增加到1000,字符串切片也会更快。只切片字符串chars[:I]
有什么不对?不需要numpy。np\u chars.dtype是dtype('你给了np.array
一个字符串,结果是一个只有一个字符串的数组。@hpaulj,chars[:I]
在i
中是线性的,不是常数(它创建了一个副本)@davidhofman,这似乎是个诀窍-谢谢。如果你碰巧对NumPy的上述奇怪行为有任何见解,即使它是一个只有一个字符串的数组,也请用“它只是一个0-D数组”来分享好的捕获。出于某种原因,我没有意识到。我很惊讶,例如len(np.array(0))
会抛出一个错误,但我想最终它是有意义的,因为例如len(0)
也会抛出一个错误。
In [96]: [chars[:i] for i in range(10)]
Out[96]:
['',
'x',
'xx',
'xxx',
'xxxx',
'xxxxx',
'xxxxxx',
'xxxxxxx',
'xxxxxxxx',
'xxxxxxxxx']
In [97]: [_95[:i] for i in range(10)]
Out[97]:
[array([], dtype='<U1'),
array(['x'], dtype='<U1'),
array(['x', 'x'], dtype='<U1'),
array(['x', 'x', 'x'], dtype='<U1'),
array(['x', 'x', 'x', 'x'], dtype='<U1'),
array(['x', 'x', 'x', 'x', 'x'], dtype='<U1'),
array(['x', 'x', 'x', 'x', 'x', 'x'], dtype='<U1'),
array(['x', 'x', 'x', 'x', 'x', 'x', 'x'], dtype='<U1'),
array(['x', 'x', 'x', 'x', 'x', 'x', 'x', 'x'], dtype='<U1'),
array(['x', 'x', 'x', 'x', 'x', 'x', 'x', 'x', 'x'], dtype='<U1')]
In [98]: timeit [chars[:i] for i in range(10)]
1.83 µs ± 23.3 ns per loop (mean ± std. dev. of 7 runs, 1000000 loops each)
In [99]: timeit [_95[:i] for i in range(10)]
3.94 µs ± 121 ns per loop (mean ± std. dev. of 7 runs, 100000 loops each)
In [100]: timeit np.array(list(chars))
3.95 µs ± 107 ns per loop (mean ± std. dev. of 7 runs, 100000 loops each)
In [101]: [''.join(_95[:i]) for i in range(10)]
Out[101]:
['',
'x',
'xx',
'xxx',
'xxxx',
'xxxxx',
'xxxxxx',
'xxxxxxx',
'xxxxxxxx',
'xxxxxxxxx']
In [102]: timeit [''.join(_95[:i]) for i in range(10)]
41.5 µs ± 306 ns per loop (mean ± std. dev. of 7 runs, 10000 loops each)