gpt4 book ai didi

python - 从 numpy 数组中提取数据

转载 作者:太空宇宙 更新时间:2023-11-04 05:58:19 24 4
gpt4 key购买 nike

我有两个 numpy 数组数据

   grid_code=[1,5,8,7,8,3,40,20....]
data= Gridcode X Y LINKCODE
1 .. .. 0
1 .. .. 0
8 .. .. 100
8 .. .. 100
10 .. .. 200
10 .. .. 200
8 .. .. 111
8 .. .. 111

我写过这样的代码

  for i in grid_code:
new_list=numpy.where(data[:,0]==i)[0]
mask_list=data[new_list,:]

我想要这样的输出在单独的文件中:

    Grid code    x    Y
1 .. ..
1 .. ..

Grid code X Y
5 .. ..
5 .. ..

Grid Code X Y ** This is for the one unique link code (eg.for this time 100)
8 .. ..
8 .. ..

Grid Code X Y
7 .. ..
7 .. ..

Grid Code X Y ** this is for other link code ( this time 111)
8 .. ..
8 .. ..

我需要根据网格代码提取数据。当重复网格代码时,我遇到问题。当网格代码重复时,我需要根据数组中的第一个LINKCODE进行提取。

我现在用这个样本数据试过这样

grid_code=np.array([6,1,9,6,1,6])

data=np.array([[1,50,40,100],
[1,40,20,100],
[6,50,40,5],
[6,50,20,5],
[9,60,90,10],
[9,90,00,10],
[6,100,100,101],
[6,50,90,101],
[6,101,10,101],
[1,11,11,11],
[1,10,10,11],
[6,200,200,102],
[6,200,200,102]])

new=[]
unique=[]
for i in grid_code:
new_list=numpy.where(data[:,0]==i)[0]
mask_list=data[new_list,:]
unique_mask=numpy.unique(mask_list[:,3]).tolist()
if len(unique_mask)>1:
unique.append([i])
new_unique=np.array(unique)
nq=new_unique[np.where(new_unique[:,0]==i)[0],:]
if len(nq)>=1:
b=len(nq)
a=b-1
for j in range(a,b):
p_list=np.where(mask_list[:,3]==unique_mask[j])[0]
n_list=mask_list[p_list,:]
print n_list

else:
print mask_list

请查看此代码并建议是否有任何有效的方法来获得相同的输出。

最佳答案

如果您正在寻求一种快速、干净且可能不会过分高效的解决方案,这是我的建议

# ev is for everything, let's start with an empty dict
ev = {}

# we identify and label all our stuff
for row in data:

# unpack a row of the data matrix
idx1, x, y, idx2 = row

# the new value for key=idx1 is {} (empty dict) if first time we see
# idx1, otherwise we keep the old value
ev[idx1] = ev.get(idx1,{})

# the new value for key=idx2 of the dictionary that is the value
# for key=idx1 is the old/default value (a list/the null list)
# plus a tuple (x,y)
ev[idx1][idx2] = ev[idx1].get(idx2,[]).append((x,y))

# we output our carefully labeled collection of x,y data
for idx1 in keys(ev):
for idx2, xys in ev[idx1]:

# the choice of the filename is subjective, isn;t it?
f = open("file%d_%d.out" % (idx1,idx2), "w")

for x, y in xys:
f.write("%d %g %g" % (idx1, x, y))

f.close()

关于python - 从 numpy 数组中提取数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26564460/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com