Search code examples
pythondictionarycluster-analysis

How to reorder a list based on the results of another list?


I have a list that was sorted into clusters or groups based on their size.

l = [4, 55, 8, 9, 12, 11, 11, 813, 832, 774, 781, 5, 769, 22]

clusters = [0, 3, 0, 0, 0, 0, 0, 1, 1, 2, 2, 0, 2, 0]

However, the sorting is wrong. There is no way to modify a base function of a package, so I wanted to reorder the clusters so that it renumbers them in an ascending numerical order.

This is the result I want:

l = [4, 55, 8, 9, 12, 11, 11, 813, 832, 774, 781, 5, 769, 22]

clusters = [0, 1, 0, 0, 0, 0, 0, 3, 3, 2, 2, 0, 2, 0]

As you can see, how the items in the list are associated with a unique cluster shouldn't change- just the number associated with the cluster should be reordered by ascending order according to the items in the list. Is there a way I can do this?

Edit: Just to clarify, I am trying to order and put 4, 5, 8, 11, 11, 12, 22 in cluster 0, 55 in cluster 1, 769, 774, 781 in cluster 2, 813, 832 in cluster 3 unlike how they are in the original cluster list.


Solution

  • First note, please do NOT override python default keywords like list to an object. Also, not sure why you tagged numpy here. I assume your objects are lists. Here is a simple solution to achieve your exact output:

    l = [4, 55, 8, 9, 12, 11, 11, 813, 832, 774, 781, 5, 769, 22]
    clusters = [0, 3, 0, 0, 0, 0, 0, 1, 1, 2, 2, 0, 2, 0]
    
    #make a dict from clusters to list
    x = dict(zip(clusters,l))
    #{0: 22, 3: 55, 1: 832, 2: 769}
    
    #sort them by values
    y = dict(sorted(x.items(),key=lambda item:item[1]))
    #{0: 22, 3: 55, 2: 769, 1: 832}
    
    #remap clusters
    clusters = list(map(dict(zip(y,range(len(y)))).get, clusters))
    #[0, 1, 0, 0, 0, 0, 0, 3, 3, 2, 2, 0, 2, 0]