Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pairing list items in a list [duplicate]

Tags:

python

numpy

I am trying to pair elements of a list based on a condition.

If two element have a common i will merge them and do this until no elements can be merged. Currently, my problem is looping through same elements and getting same merged result from different items. I have to check if group has been added before.But as my array is empty in the beginning i could not check if element already in it with axis 1. I tried recursive : Also i am discarding if a group has length less than three.

pairs = [[1, 3], [1, 8], [2, 1], [2, 3], [3, 1], [3, 8], [4, 11], [4, 15], [7, 13], [9, 12], [9, 13], [10, 1], [10, 18], [10, 20], ...]


def groupG(pairs):
    groups = []
    if len(pairs) > 1:
        for i,pair in enumerate(pairs):
            try:
                if (any(point in pairs[i+1] for point in pair)):
                    group = np.concatenate(( pair,pairs[i+1]))
                    group = np.unique(group)
                    groups.append(group)
            except IndexError:
                continue
            
    if len(groups) == 0 :
        groupsFiltered = np.array([row for row in pairs if len(row)>=3])
        return groupsFiltered
    else: 
        return groupG(groups)

expected result is :

[[1,2,3,8,10,18,20],[4,11,15],[7,9,12,13]...]

Is there a way to group these pairs with while,do while or recursive?

like image 495
Elif Avatar asked Oct 13 '25 11:10

Elif


2 Answers

Use networkx's connected_components:

import networkx as nx

pairs = [[1, 3], [1, 8], [2, 1], [2, 3], [3, 1], [3, 8], [4, 11], [4, 15],
         [7, 13], [9, 12], [9, 13], [10, 1], [10, 18], [10, 20]]

out = list(nx.connected_components(nx.from_edgelist(pairs)))

output:

[{1, 2, 3, 8, 10, 18, 20}, {4, 11, 15}, {7, 9, 12, 13}]
like image 156
mozway Avatar answered Oct 15 '25 05:10

mozway


Well, you can do this :

def group_pairs(available_pairs):
    # check if there was any new merge to break the loop otherwise
    is_modified = True

    # a while loop on the 'is_modified' condition
    while is_modified:
        # init the new available pairs, and the pair to start the merging with
        new_pairs = available_pairs.copy()
        merged_pairs = available_pairs[0]

        #merge if eny similarity is noticed
        for i in range(len(available_pairs)):
            if any(el in available_pairs[i] for el in merged_pairs):
                merged_pairs += available_pairs[i]
                new_pairs.remove(available_pairs[i])

        # add the merged data
        new_pairs.append(list(set(merged_pairs)))

        # update the while checker
        is_modified = len(new_pairs) != len(available_pairs)

        # update the available pairs
        available_pairs = new_pairs.copy()
    
    return available_pairs

# a little test
pairs = [[1, 3], [1, 8], [2, 1], [2, 3], [3, 1], [3, 8], [4, 11], [4, 15],
         [7, 13], [9, 12], [9, 13], [10, 1], [10, 18], [10, 20]]

print(group_pairs(pairs))

output:

[[11, 4, 15], [9, 12, 13, 7], [1, 2, 3, 8, 10, 18, 20]]
like image 45
mrCopiCat Avatar answered Oct 15 '25 04:10

mrCopiCat