简体   繁体   English

查找两个大小不同的数组的所有匹配项

[英]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 . 我有一个定义的函数d(x,y) ,它给出两个元素xy之间的差。 They match as such: each element in list1 matches to either one or zero elements in list2. 它们如此匹配:list1中的每个元素都匹配list2中的一个或零个元素。 Unmatched elements also have a "difference" given by d(a) . 不匹配的元素还具有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. 如果相关,我正在使用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. 我认为您希望在二部图中进行匹配: http : //en.wikipedia.org/wiki/Matching_( graph_theory) #Maximum_matchings_in_bipartite_graphs您应该使用匹配算法。 If you can't then as a last resort use integer programming. 如果不能,那么请使用整数编程。 Pulp is a suitable python package for integer programming. Pulp是适合整数编程的python软件包。 The Integer programming package will be much slower but perhaps easier to code. Integer编程包会慢很多,但可能更容易编写代码。

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表示list1 [i]和list2 [j]之间的连接。 Aij = 1 or 0. (connection either on or off). Aij = 1或0。(连接打开或关闭)。 Sum(Aij over i) = 1. (one connection per element in list1). Sum(i上的Aij)=1。(list1中每个元素一个连接)。 Sum(Aij over j) = 1 (one connection per element in list2). Sum(j上的Aij)= 1(列表2中每个元素一个连接)。 Now you want to maximize/minimize the objective function sum(d(list1[i], list2[j])*Aij). 现在您要最大化/最小化目标函数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. 不要忘记,如果length list1!= length list2,则必须添加额外的变量,以允许某些变量以成本d(x)连接到自身,并将其添加到目标函数中。

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. 尽管我想知道OP是否正在寻找列表或类似产品。

Perhaps if the OP looked through itertools-functions they might see something suitable. 也许,如果OP通过itertools函数进行浏览,他们可能会看到一些合适的东西。

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]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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