【发布时间】:2018-12-11 11:40:34
【问题描述】:
我想创建一个数组来保存混合类型 - 字符串和整数。
以下代码未按预期工作 - 所有元素都输入为字符串。
>>> a=numpy.array(["Str",1,2,3,4])
>>> print a
['Str' '1' '2' '3' '4']
>>> print type(a[0]),type(a[1])
<type 'numpy.string_'> <type 'numpy.string_'>
数组的所有元素都输入为“numpy.string_”
但是,奇怪的是,如果我将其中一个元素作为“无”传递,则类型会按预期显示:
>>> a=numpy.array(["Str",None,2,3,4])
>>> print a
['Str' None 2 3 4]
>>> print type(a[0]),type(a[1]),type(a[2])
<type 'str'> <type 'NoneType'> <type 'int'>
因此,包含“无”元素为我提供了一种解决方法,但我想知道为什么会这样。 即使我没有将其中一个元素作为 None 传递,难道不应该在传递元素时输入它们吗?
【问题讨论】:
-
两者都不是真正重复的,第二个更好,但是关于
None的更明确的解释对于OP来说会更好 -
建议的副本只解释了字符串 dtype:stackoverflow.com/questions/49751000/…。
标签: python arrays numpy types mixed