python—对for循环中的数组应用掩码
我有以下代码:python—对for循环中的数组应用掩码,python,numpy,dictionary,Python,Numpy,Dictionary,我有以下代码: import numpy as np result = {} result['depth'] = [1,1,1,2,2,2] result['generation'] = [1,1,1,2,2,2] result['dimension'] = [1,2,3,1,2,3] result['data'] = [np.array([0,0,0]), np.array([0,0,0]), np.array([0,0,0]), np.array([0,0,0]), np.array([0
import numpy as np
result = {}
result['depth'] = [1,1,1,2,2,2]
result['generation'] = [1,1,1,2,2,2]
result['dimension'] = [1,2,3,1,2,3]
result['data'] = [np.array([0,0,0]), np.array([0,0,0]), np.array([0,0,0]), np.array([0,0,0]), np.array([0,0,0]), np.array([0,0,0])]
for v in np.unique(result['depth']):
temp_v = (result['depth'] == v)
values_v = [result[string][temp_v] for string in result.keys()]
this_v = dict(zip(result.keys(), values_v))
在其中,我想创建一个名为“this_v”的新dict,其键与原始dict结果相同,但值较少
该行:
values_v = [result[string][temp_v] for string in result.keys()]
给出一个错误
TypeError:只能将整数标量数组转换为标量索引
我不明白,因为我可以创建ex=result[result.keys[0]][temp_v]很好。它只是不允许我使用for循环来完成这项工作,这样我就可以填充列表
你知道为什么它不起作用吗?为了解决查找和删除副本的问题,我鼓励你使用熊猫。它是一个Python模块,让您的生活变得非常简单:
import numpy as np
result = {}
result['depth'] = [1,1,1,2,2,2]
result['generation'] = [1,1,1,2,2,2]
result['dimension'] = [1,2,3,1,2,3]
result['data'] = [np.array([0,0,0]), np.array([0,0,0]), np.array([0,0,0]),\
np.array([0,0,0]), np.array([0,0,0]), np.array([0,0,0])]
# Here comes pandas!
import pandas as pd
# Converting your dictionary of lists into a beautiful dataframe
df = pd.DataFrame(result)
#> data depth dimension generation
# 0 [0, 0, 0] 1 1 1
# 1 [0, 0, 0] 1 2 1
# 2 [0, 0, 0] 1 3 1
# 3 [0, 0, 0] 2 1 2
# 4 [0, 0, 0] 2 2 2
# 5 [0, 0, 0] 2 3 2
# Dropping duplicates... in one single command!
df = df.drop_duplicates('depth')
#> data depth dimension generation
# 0 [0, 0, 0] 1 1 1
# 3 [0, 0, 0] 2 1 2
如果你想让你的数据恢复原始格式。。。您还需要一行代码
df.to_dict('list')
#> {'data': [array([0, 0, 0]), array([0, 0, 0])],
# 'depth': [1, 2],
# 'dimension': [1, 1],
# 'generation': [1, 2]}
请记住,所有dict值都是列表,而不是NumPy数组。将它们转换为适当的NumPy数组可能会改变一些事情。只需为您添加np.array dict值。熊猫已经成为我的新信仰!