更新:你可能想调查
Pandas.它
系列和DataFrame比NumPy更易于使用且功能更全面
结构化数组.
import numpy as np
channel = np.zeros(1, dtype = [('PRN',int),
('acquiredFreq',int),
('codePhase',int),
('status','|S1')])
print(channel)
# [(0, 0, 0, '')]
通过整数索引访问特定行:
print(channel[0])
# (0, 0, 0, '')
按列名索引将列作为数组返回:
print(channel['PRN'])
# [0]
或者你可以遍历每一行并分配给每个字段(列),
但这在NumPy中相对较慢.
for row in channel:
row['PRN'] = 1
row['acquiredFreq'] = 1
row['codePhase'] = 1
row['status'] = '+'
print(channel)
# [(1, 1, 1, '+')]
为了完整起见,我还要提到你可以按行分配列:
channel[0]['status'] = '-'
print(channel)
# [(1, 1, 1, '-')]
或按列分配然后行:
channel['PRN'][0] = 10
print(channel)
# [(10, 1, 1, '-')]
我展示了上述内容,因为它与您发布的Matlab代码最相似.但是,让我再次强调,分配给NumPy数组中的单个单元格很慢.以上NumPy方式是做整数组赋值:
channel['PRN'] = PRNindexes
其中PRNindexes是一个序列(例如列表,元组或NumPy数组).
您还可以使用fancy indexing(也称为“高级索引”)来选择行:
index = (channel.status == '+') # Select all rows with status '+'
channel['PRN'][index] = 10 # Set PRN to 10 for all those rows
请记住,花哨的索引会返回一个新数组,而不是原始数组的视图. (相反,“基本切片”(例如通道[0]或通道[1:10]返回一个视图.)因此,如果要进行更改原始数组的赋值,请先按列选择,然后选择花式索引(索引)
channel['PRN'][index] = ...
而不是
channel[index]['PRN'] = ...