Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create a mapping of elements from two arrays

I have two arrays which are outputs of a clustering algorithm. Is there a possibility of an automatic way to find the associative mapping.

Consider two label arrays:

array1 = [0,0,1,2,3]
array2 = [4,4,6,8,7]

Visually these look the same, but for a larger label set, I want to find a mapping like {0:4,1:6,2:8,3:7}.

Does Python have any method to do this?

I have looked at sklearn metrics for similar solution, no luck yet. Any help would be appreciated.

like image 699
Rohan Mohapatra Avatar asked Nov 17 '25 09:11

Rohan Mohapatra


1 Answers

Yes, you can create a dictionary with the key as the element of the first list and value as the element on the second list, by zipping the two lists together, and converting the output to a dictionary

array_1 = [0,0,1,2,3]
array_2 = [4,4,6,8,7]

#Zip the two lists together, and create a dictionary out of the zipped lists
mapping = dict(zip(array_1, array_2))
print(mapping)

The output will be

{0: 4, 1: 6, 2: 8, 3: 7}

Note that if you have duplicate elements in array_1 but the corresponding elements in array_2 are different, the last element of array_2 will be picked in the mapping for the duplicate elements, e.g for [0,0,1,1] and [4,5,6,7]. the mapping will be {0: 5, 1: 7}, since 5 is picked for duplicate element 0 and 7 is picked for duplicate element 1

like image 110
Devesh Kumar Singh Avatar answered Nov 20 '25 01:11

Devesh Kumar Singh



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!