Python 我可以在压缩记录数据类型创建中指定字段名吗?

Python 我可以在压缩记录数据类型创建中指定字段名吗?,python,numpy,types,Python,Numpy,Types,在numpy中,dtype('i4,(5)f8')是dtype([('f0','p> 具有用户定义字段名的几个示例: Using array-protocol type strings: >>> np.dtype([('a','f8'),('b','S10')]) 但没有证据表明向“i4,(5)f8”输入中添加名称的语法。下一步是查看是否可以找到解析dtype输入的代码 numpy/core/records.py似乎完成了大部分的dtype创建。numpy/core/\u

在numpy中,
dtype('i4,(5)f8')
dtype([('f0','p>

具有用户定义字段名的几个示例:

Using array-protocol type strings:
>>> np.dtype([('a','f8'),('b','S10')])

但没有证据表明向“i4,(5)f8”输入中添加名称的语法。下一步是查看是否可以找到解析
dtype
输入的代码

numpy/core/records.py
似乎完成了大部分的
dtype
创建。
numpy/core/\u internal.py
\u commastring
解析“捷径”

它使用:

format_re = re.compile(asbytes(
                       r'(?P<order1>[<>|=]?)'
                       r'(?P<repeats> *[(]?[ ,0-9L]*[)]? *)'
                       r'(?P<order2>[<>|=]?)'
                       r'(?P<dtype>[A-Za-z0-9.]*(?:\[[a-zA-Z0-9,.]+\])?)'))
format\u re=re.compile(asbytes(
r'(?P[|=]?)'
r'(?P*[(]?[,0-9L]*[)]?*)'
r'(?P[|=]?)'
r'(?P[A-Za-z0-9.]*(?:\[[A-Za-z0-9,.]+\]]))
解析字符串。它不查找
名称
。我猜它是为
numarray
编写的,这是一个
numpy
的前身,没有使用字段名。编写自己的解析器应该不难。:)

Using dictionaries. Two fields named ‘gender’ and ‘age’:
>>> np.dtype({'names':['gender','age'], 'formats':['S1',np.uint8]})
format_re = re.compile(asbytes(
                       r'(?P<order1>[<>|=]?)'
                       r'(?P<repeats> *[(]?[ ,0-9L]*[)]? *)'
                       r'(?P<order2>[<>|=]?)'
                       r'(?P<dtype>[A-Za-z0-9.]*(?:\[[a-zA-Z0-9,.]+\])?)'))