Python 在numpy中强制转换到数组时,列表元素的截断违反直觉?

Python 在numpy中强制转换到数组时,列表元素的截断违反直觉?,python,numpy,scipy,Python,Numpy,Scipy,我注意到numpy中数组的这种违反直觉的行为。我有一个列表,我想将其转换为数组: >>> a = [['abc', 117858348, 117858388, 'def']] 当我将其转换为数组时,它将元素转换为字符串(这很好),但意外地删除了中间两个元素的最后一位: >>> array(a) array([['abc', '11785834', '11785838', 'def']], dtype='|S8') 这是什么原因?有没有办法避免

我注意到
numpy
中数组的这种违反直觉的行为。我有一个列表,我想将其转换为数组:

>>> a = [['abc', 117858348, 117858388, 'def']]
当我将其转换为数组时,它将元素转换为字符串(这很好),但意外地删除了中间两个元素的最后一位:

>>> array(a)
array([['abc', '11785834', '11785838', 'def']], 
      dtype='|S8')
这是什么原因?有没有办法避免这种行为?将列表列表转换为数组比较方便的原因是为了快速索引某些元素。例如,如果将索引列表
x
放入数组
a
,则可以执行
a[x]
来检索它们。如果
a
是一个列表列表,则不能,而是必须执行类似
[a[i]for i in x]
的操作


谢谢。

这很有趣,运行您的示例可以告诉我:

>>> numpy.asarray([['abc', 117858348, 117858388, 'def']])
array([['abc', '117', '117', 'def']], 
      dtype='|S3')
我很想知道转换是如何进行的:

>>> help(numpy.asarray)
asarray(a, dtype=None, order=None)
Convert the input to an array.

Parameters
----------
a : array_like
    Input data, in any form that can be converted to an array.  This
    includes lists, lists of tuples, tuples, tuples of tuples, tuples
    of lists and ndarrays.
dtype : data-type, optional
    By default, the data-type is inferred from the input data.
看起来基础类型是从输入数据推断的
,我不知道这意味着什么,所以我这么做了

>>> import inspect
>>> print inspect.getsource(numpy.asarray)
我们得到了
返回数组(a,dtype,copy=False,order=order)
但是
numpy.array是内置的,因此通过我们得到的文档:

数据类型:数据类型,可选
阵列所需的数据类型。如果未给出,则类型将被确定为在序列中保存对象所需的最小类型。此参数只能用于“向上投射”数组。对于向下广播,请使用.astype(t)方法

它看起来像是在可能的情况下向上投射,所以在我的例子中是向上投射到长度为3的字符串,因为这是序列中我拥有的最长的字符串,如果我引入一个更长的字符串,它会向上投射到该字符串,在我的例子中,它似乎没有正确地考虑到其他类型的数字长度,这可能是一个bug,我不知道

您可以只指定一个长字符串序列

>>> numpy.asarray([['abc', 117858348, 117858388, 'defs']], dtype = 'S20')
array([['abc', '117858348', '117858388', 'defs']], 
  dtype='|S20')
20个字符似乎就足够了,尽管它可能会消耗更多内存,所以您可以简单地将其设置为最大值

据我所知,
numpy
将值存储为同质类型,这就是为什么在创建数组时所有内容都必须是预先确定的类型

>>> numpy.__version__
'1.6.1'

$ python --version
Python 2.6.1

$ uname -a
Darwin 10.8.0 Darwin Kernel Version 10.8.0: Tue Jun  7 16:33:36 PDT 2011; root:xnu-1504.15.3~1/RELEASE_I386 i386

我希望这能有所帮助。

这很有趣,运行您的示例可以告诉我:

>>> numpy.asarray([['abc', 117858348, 117858388, 'def']])
array([['abc', '117', '117', 'def']], 
      dtype='|S3')
我很想知道转换是如何进行的:

>>> help(numpy.asarray)
asarray(a, dtype=None, order=None)
Convert the input to an array.

Parameters
----------
a : array_like
    Input data, in any form that can be converted to an array.  This
    includes lists, lists of tuples, tuples, tuples of tuples, tuples
    of lists and ndarrays.
dtype : data-type, optional
    By default, the data-type is inferred from the input data.
看起来基础类型是从输入数据推断的
,我不知道这意味着什么,所以我这么做了

>>> import inspect
>>> print inspect.getsource(numpy.asarray)
我们得到了
返回数组(a,dtype,copy=False,order=order)
但是
numpy.array是内置的,因此通过我们得到的文档:

数据类型:数据类型,可选
阵列所需的数据类型。如果未给出,则类型将被确定为在序列中保存对象所需的最小类型。此参数只能用于“向上投射”数组。对于向下广播,请使用.astype(t)方法

它看起来像是在可能的情况下向上投射,所以在我的例子中是向上投射到长度为3的字符串,因为这是序列中我拥有的最长的字符串,如果我引入一个更长的字符串,它会向上投射到该字符串,在我的例子中,它似乎没有正确地考虑到其他类型的数字长度,这可能是一个bug,我不知道

您可以只指定一个长字符串序列

>>> numpy.asarray([['abc', 117858348, 117858388, 'defs']], dtype = 'S20')
array([['abc', '117858348', '117858388', 'defs']], 
  dtype='|S20')
20个字符似乎就足够了,尽管它可能会消耗更多内存,所以您可以简单地将其设置为最大值

据我所知,
numpy
将值存储为同质类型,这就是为什么在创建数组时所有内容都必须是预先确定的类型

>>> numpy.__version__
'1.6.1'

$ python --version
Python 2.6.1

$ uname -a
Darwin 10.8.0 Darwin Kernel Version 10.8.0: Tue Jun  7 16:33:36 PDT 2011; root:xnu-1504.15.3~1/RELEASE_I386 i386

我希望这能有所帮助。

如果使用对象数组,就不会有任何截断。这也将允许您混合不同的类型,并获得所有索引

a = [['abc', 117858348, 117858388, 'def']]
a = array(a, dtype=object)
type(a[0, 0])
# <type str>
type(a[0, 1])
# <type int>
a=[['abc',117858348,1178583888,'def']]
a=数组(a,数据类型=对象)
类型(a[0,0])
# 
类型(a[0,1])
# 

如果使用对象数组,则不会进行任何截断。这也将允许您混合不同的类型,并获得所有索引

a = [['abc', 117858348, 117858388, 'def']]
a = array(a, dtype=object)
type(a[0, 0])
# <type str>
type(a[0, 1])
# <type int>
a=[['abc',117858348,1178583888,'def']]
a=数组(a,数据类型=对象)
类型(a[0,0])
# 
类型(a[0,1])
#