4

having failed to post the first question in a clear way, I will try to be more specific.
I have two 2d matrices, one with 5 columns (let's call it data) and the other with 7 (let's call it BMU). Let's call the 5 column submatrix photometry and the last two columns respectively z and z_err.
What I would like to do is, excluded the last two columns of BMU, calculate all the euclidean distances between the data objects and the BMU objects, then for each data objects find the closest k BMU objects. (here I rearrange the distance matrix and so I can't associate a distance to a specific BMU object)
Done so, I would like to retrieve this closest k objects in the BMU matrix and use the 6th and 7th column to perform some operation.
In order to store and retrieve all these informations that I need, I was trying to create some sort of dictionary. So, when I calculate the distances, I can use an ID to associate the particular distance to the features of the BMu object (photometry, z and z_err) even if I have to rearrange them.

thanks for any help :)

Bradipo Eremita
  • 83
  • 1
  • 1
  • 9
  • dict(zip(range(len(BMU)), BMU[:,:-2])) This will create a dictionary but I can't manage to separate the photometry from the last two columns and add them as values separatelly – Bradipo Eremita Dec 12 '16 at 11:04

2 Answers2

14

You can use zip and dict

Example:

dictionary = dict(zip(keys, values))
7

Sembei Norimaki has given the perfect answer, it is more concise than my loop. I have edited my answer to give some additional value.

I would use the zip function and a generator

{key: value for (key, value) in zip(keys, values)}

This has the advantage that you can add additional conditions. Maybe you want to skip some keys ?

{key: value for (key, value) in zip(keys, values) if ...}

In a loop, this would look like:

keys=["a", "b", "c"]
vals=[1, 2, 3]

new_dict={}

for key, val in zip(keys, vals):

    # check invariants on key and value,
    # skip if the tests fail
    if not do_some_tests(key, val):
        continue
    new_dict[key]=val
lhk
  • 27,458
  • 30
  • 122
  • 201