Skip to content

Instantly share code, notes, and snippets.

@jfburkhart
Created July 13, 2011 09:28
Show Gist options
  • Save jfburkhart/1079991 to your computer and use it in GitHub Desktop.
Save jfburkhart/1079991 to your computer and use it in GitHub Desktop.
concatenation to fill a dict
def remove_dups(x, y, z):
"""
## for regridding need to remove any duplicates
## First make sure there is enough data
"""
if len(z) > 10:
vals = zip(x, y, z)
vals.sort()
z0 = zip(x, y)
dummy = {}
for i, a in enumerate(vals):
if z0[i] not in dummy:
dummy.setdefault((a[0], a[1], a[2]))
#pdb.set_trace()
x = []
y = []
z = []
for tmp in dummy:
x.append(tmp[0])
y.append(tmp[1])
z.append(dummy[tmp])
x = np.array(x)
y = np.array(y)
z = np.array(z)
else:
raise Warning, "Not enough data to remove dups"
return x, y, z
@jfburkhart
Copy link
Author

improved per bignose suggestions:

def remove_dups(original_sequence):
"""
## for regridding need to remove any duplicates

"""
z_values_by_xy = collections.defaultdict(list)
for (x, y, z) in original_sequence:
    z_values_by_xy[(x, y)].append(z)

return [(x, y, np.mean(z_values_by_xy[(x,y)])) for (x, y) in z_values_by_xy]

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment