Skip to content

Instantly share code, notes, and snippets.

@AdamBrouwersHarries
Created December 2, 2016 14:28
Show Gist options
  • Save AdamBrouwersHarries/366cf662a0da0d307385339029c5c433 to your computer and use it in GitHub Desktop.
Save AdamBrouwersHarries/366cf662a0da0d307385339029c5c433 to your computer and use it in GitHub Desktop.
zipping indexed sparse lists in python
arra = [(1.0, 0.5), (3.4, 1), (9.1, 5)]
arrb = [(0.3, 1), (2.0, 2), (4.7, 6)]
arrc = [(0.1, 2), (0.3, 3), (5.7, 7)]
arrd = [(1.1, 3), (2.1, 4), (3.3, 8)]
def index(x):
return x[0]
def data(x):
return x[1]
def set_data(lis, idx, data):
lis[idx] = (index(lis[idx]), data)
def zip(arrays):
result = [arrays[0]]
for arr in arrays[1:]:
# find the missing datapoints from the other arrays to extend ours
# we only need to check the first one, as it should be always extended
# when we add a new array
newarr = zip_arrs(arr, result[0])
# now add all our datapoints to all the other arrays
for i in range(0, len(result)):
result[i] = zip_arrs(result[i], arr)
# and add our new array
result.append(newarr)
return result
def zip_arrs(target, source):
# build a dict from the target
t_dict = dict(target)
# iterate over the source, and add to the dict
for elem in source:
if not index(elem) in t_dict:
t_dict[index(elem)] = "undefined"
# serialise the t_dict back to a list
t_list = sorted(t_dict.items(), key=index)
# iterate over it, and where the entry is "undefined", average the points
# on the other sides
# print t_list
# for i in range(0, len(t_list)):
# print "ITERATION"
# if data(t_list[i]) == "undefined":
# if i == 0:
# print "first element"
# # find the next highest, not-undefined datapoint
# j = i + 1
# while data(t_list[j]) == "undefined":
# print "data undefined"
# j = j + 1
# print "setting: " + str(t_list[j])
# set_data(t_list, i, data(t_list[j]))
# elif i == len(t_list)-1:
# set_data(t_list, i, data(t_list[i-1]))
# else:
# # find the next highest, not-undefined datapoint
# j = i + 1
# while j < len(t_list) and data(t_list[j]) == "undefined":
# j = j + 1
# if j < len(t_list):
# # get current, previous and next indexs
# current_index = index(t_list[i])
# previous_index = index(t_list[i-1])
# next_index = index(t_list[j])
# print "current index: " + str(current_index)
# print "previous index: " + str(previous_index)
# print "next index: " + str(next_index)
# # get previous and next datapoints
# previous_data = data(t_list[i-1])
# next_data = data(t_list[j])
# print "previous data: " + str(previous_data)
# print "next data: " + str(next_data)
# # get "how far between" we are, as a fraction
# fractional_dist = \
# (current_index-previous_index) / (next_index-previous_index)
# # get how far between the previous and next data we are
# our_data = \
# previous_data + ((next_data-previous_data) / fractional_dist)
# set_data(t_list,i,our_data)
# else:
# # give up and just use the previous datapoint
# set_data(t_list,i, data(t_list[i-1]))
return t_list
def main():
print zip_arrs(arra, arrb)
zipped = zip([arra, arrb, arrc, arrd])
for z in zipped:
print z
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment