python-将掩码应用于for循环中的数组 [英] python - applying a mask to an array in a for loop

查看:69
本文介绍了python-将掩码应用于for循环中的数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有此代码:

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))

其中我要创建一个新的 dict ,称为 this_v ',其键与原始字典结果相同,但值较少。

in which I want to create a new dictcalled 'this_v', with the same keys as the original dict result, but fewer values.

该行:

values_v = [result[string][temp_v] for string in result.keys()]

出现错误


TypeError:仅整数标量数组c可以转换为标量索引

TypeError: only integer scalar arrays can be converted to a scalar index

我不理解,因为我可以创建 ex = result [result.keys()[0]] [temp_v] 很好。

which I don't understand, since I can create ex = result[result.keys()[0]][temp_v] just fine. It just does not let me do this with a for loop so that I can fill the list.

关于为什么它不起作用的任何想法?

Any idea as to why it does not work?

推荐答案

为了解决您的问题(查找和删除重复项),我建议您使用 pandas 。这是一个Python模块,使您的生活简直荒谬:

In order to solve your problem (finding and dropping duplicates) I encourage you to use pandas. It is a Python module that makes your life absurdly simple:

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

如果您希望将数据恢复为原始格式,则只需要一行代码即可。

If you want oyur data back in the original format... you need yet again just one line of code!

df.to_dict('list')

#> {'data': [array([0, 0, 0]), array([0, 0, 0])],
#   'depth': [1, 2],
#   'dimension': [1, 1],
#   'generation': [1, 2]}

这篇关于python-将掩码应用于for循环中的数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆