Python - Applying A Mask To An Array In A For Loop
I have this code: 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.arra
Solution 1:
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]}
Post a Comment for "Python - Applying A Mask To An Array In A For Loop"