简体   繁体   中英

Find all matchings of two differently sized arrays

So, I have two different lists, say:

list1 = [a,b,c,d] 
list2 = [e,f,g] 

And my goal is to find out the minimum difference between these two lists. I have a defined function d(x,y) which gives the difference between two elements, x and y . They match as such: each element in list1 matches to either one or zero elements in list2. Unmatched elements also have a "difference" given by d(a) .

I'm not sure what the best algorithm for doing this is, or how I'd go about it. If it's relevant, I'm working in python.

I think you want a matching in a bipartite graph: http://en.wikipedia.org/wiki/Matching_(graph_theory)#Maximum_matchings_in_bipartite_graphs You should go with the matching algorithm. If you can't then as a last resort use integer programming. Pulp is a suitable python package for integer programming. The Integer programming package will be much slower but perhaps easier to code.

If you choose to go with Integer Programming, the constraints is that a variable Aij represents a connection between list1[i] and list2[j]. Aij = 1 or 0. (connection either on or off). Sum(Aij over i) = 1. (one connection per element in list1). Sum(Aij over j) = 1 (one connection per element in list2). Now you want to maximize/minimize the objective function sum(d(list1[i], list2[j])*Aij). Don't forget if length list1 != length list2, you must add extra variables to allow for some variables to connect to themselves with cost d(x) and add that to the objective function.

I'm not quite sure if what you're asking is basically to find all the elements that are in common between the two lists.

In that case, this might work:

list1 = [1,2,3,4]
list2 = [3,4,5]
diff = set(list1).intersection(set(list2))

I think that the question is a bit vague but my stab at it would be the following:

Although I wonder if the OP is looking for the product of the lists or similar.

Perhaps if the OP looked through itertools-functions they might see something suitable.

import random

list1 = random.sample(range(50), 3)
list2 = random.sample(range(50), 2)

print "List1", list1
print "List2", list2


def router(a, b):
    print "ROUTER", a, b
    if a == None:
        return b * 10
    elif b == None:
        return a * 10
    else:
        return (a + b ) * 10


print map(router, list1, list2)

##########################
# Using itertools.product
##########################
import itertools

def pair(p):
    a, b = p
    print "PAIR", a, b
    if a == None:
        return b * 10
    elif b == None:
        return a * 10
    else:
        return (a + b ) * 10

product = itertools.product(*zip(*map(None, list1, list2)))

print map(pair, product)

Output

List1 [17, 48, 9]
List2 [45, 42]
ROUTER 17 45
ROUTER 48 42
ROUTER 9 None
[620, 900, 90]
PAIR 17 45
PAIR 17 42
PAIR 17 None
PAIR 48 45
PAIR 48 42
PAIR 48 None
PAIR 9 45
PAIR 9 42
PAIR 9 None
[620, 590, 170, 930, 900, 480, 540, 510, 90]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM