numpy 数组的元组列表 - 混合类型,unicode 转换不起作用
List of tuples to numpy array - mixed types, unicode conversion not working
我有一个 "text, integer" 格式的元组列表,其中文本是 unicode 并且来自使用 utf-8
编码打开的文件。
我正在尝试将列表转换为类型保持正确的 numpy 多维数组。
使用正常的 np.array
或 np.asarray
,将所有内容转换为 unicode 或 U1
tuples = [("A",1),("B",2)]
np.array(tuples)
>>> [['A' '1']
['B' '2']]
指定我想要的类型给我一个空字符串而不是多维:
np.array(tuples, dtype=np.dtype("U,int"))
>>> [('', 1) ('', 2)]
除非我为 unicode 指定了一些字符,但我不能这样做,因为我的文本是可变长度的。这也不是多维的。
np.array(tuples, dtype=np.dtype("<U99,int"))
>>> [('A', 1) ('B', 2)]
如何将我的元组列表转换为具有整数和字符串格式的 numpy 数组?此外,最终,我将在第二列上执行 np.where()
并检查值是否大于例如50,因此适用于下一步的解决方案将是理想的。谢谢!
编辑:理想情况下,结果应该是一个多维数组,以便在需要时可以自行选择各个列
按照链接问题 (numpy recarray strings of variable length, Automatic string length in recarray) 中的建议,将第一种类型声明为 object
:
np.array([("A", 1), ("B", 2)], dtype=[('str', np.object), ('int', np.int)])
select 一个合理的 unicode 字符串长度并不难。
In [172]: alist = [('A',1),('Beta', 2), ('Gamma', 3)]
例如,获取列表中字符串的长度:
In [173]: dt1 = np.array([row[0] for row in alist]).dtype
In [174]: dt1
Out[174]: dtype('<U5')
In [175]: dt = np.dtype([('label',dt1),('value', int)])
In [176]: dt
Out[176]: dtype([('label', '<U5'), ('value', '<i8')])
In [177]: arr = np.array(alist, dt)
In [178]: arr
Out[178]:
array([('A', 1), ('Beta', 2), ('Gamma', 3)],
dtype=[('label', '<U5'), ('value', '<i8')])
使用名称字段:
In [179]: arr['label']
Out[179]: array(['A', 'Beta', 'Gamma'], dtype='<U5')
In [180]: arr['value']%2==1
Out[180]: array([ True, False, True])
通常我们不需要多维结构化数组。
如果数据在 csv 样式文件中,我们可以让 genfromtxt
选择字符串字段大小:
In [181]: txt = """A 1
...: Beta 2
...: Gamma 3"""
In [182]: data = np.genfromtxt(txt.splitlines(), dtype=None,encoding=None)
In [183]: data
Out[183]:
array([('A', 1), ('Beta', 2), ('Gamma', 3)],
dtype=[('f0', '<U5'), ('f1', '<i8')])
我有一个 "text, integer" 格式的元组列表,其中文本是 unicode 并且来自使用 utf-8
编码打开的文件。
我正在尝试将列表转换为类型保持正确的 numpy 多维数组。
使用正常的 np.array
或 np.asarray
,将所有内容转换为 unicode 或 U1
tuples = [("A",1),("B",2)]
np.array(tuples)
>>> [['A' '1']
['B' '2']]
指定我想要的类型给我一个空字符串而不是多维:
np.array(tuples, dtype=np.dtype("U,int"))
>>> [('', 1) ('', 2)]
除非我为 unicode 指定了一些字符,但我不能这样做,因为我的文本是可变长度的。这也不是多维的。
np.array(tuples, dtype=np.dtype("<U99,int"))
>>> [('A', 1) ('B', 2)]
如何将我的元组列表转换为具有整数和字符串格式的 numpy 数组?此外,最终,我将在第二列上执行 np.where()
并检查值是否大于例如50,因此适用于下一步的解决方案将是理想的。谢谢!
编辑:理想情况下,结果应该是一个多维数组,以便在需要时可以自行选择各个列
按照链接问题 (numpy recarray strings of variable length, Automatic string length in recarray) 中的建议,将第一种类型声明为 object
:
np.array([("A", 1), ("B", 2)], dtype=[('str', np.object), ('int', np.int)])
select 一个合理的 unicode 字符串长度并不难。
In [172]: alist = [('A',1),('Beta', 2), ('Gamma', 3)]
例如,获取列表中字符串的长度:
In [173]: dt1 = np.array([row[0] for row in alist]).dtype
In [174]: dt1
Out[174]: dtype('<U5')
In [175]: dt = np.dtype([('label',dt1),('value', int)])
In [176]: dt
Out[176]: dtype([('label', '<U5'), ('value', '<i8')])
In [177]: arr = np.array(alist, dt)
In [178]: arr
Out[178]:
array([('A', 1), ('Beta', 2), ('Gamma', 3)],
dtype=[('label', '<U5'), ('value', '<i8')])
使用名称字段:
In [179]: arr['label']
Out[179]: array(['A', 'Beta', 'Gamma'], dtype='<U5')
In [180]: arr['value']%2==1
Out[180]: array([ True, False, True])
通常我们不需要多维结构化数组。
如果数据在 csv 样式文件中,我们可以让 genfromtxt
选择字符串字段大小:
In [181]: txt = """A 1
...: Beta 2
...: Gamma 3"""
In [182]: data = np.genfromtxt(txt.splitlines(), dtype=None,encoding=None)
In [183]: data
Out[183]:
array([('A', 1), ('Beta', 2), ('Gamma', 3)],
dtype=[('f0', '<U5'), ('f1', '<i8')])