سؤال

Sorry for the obscurity of the question, but I was wondering, how can I compare two sets of numbers, and see which ones satisfy an equation? For example:

a = [1,2,3,4,5,6]
b = [7,8,9,10,11,12]

I need something that looks at the two lists, then sees which ones satisfy an equation like a + b = 12. It needs to return all the pairs of numbers that satisfy an equation.

EDIT: I want it to compare every combination of the two lists! Sorry!

هل كانت مفيدة؟

المحلول

zip is your friend:

good_vals = [(aa, bb) for aa, bb in zip(a, b) if aa + bb == 12]

EDIT

Since it looks like you want to do an all-to-all comparison, then you'll need a nested loop -- either explicit or implied. Here are some options:

# itertools.product
[tup for tup in it.product(a, b) if sum(tup) == 12]

# nested list-comp
[(aa, bb) for aa in a for bb in b if aa + bb == 12]

# good ole' fashioned loop:
result = []
for aa in a:
    for bb in b:
        if aa + bb == 12:
            result.append((aa, bb))

Some might wonder why I include the last option ... Indeed, it is much more verbose and typically less efficient than the other two. However, in some cases, you might be able to continue the outer loop without doing the inner loop at all ... e.g. if aa > 12 and you know that bb is always positive due to some constraint on the problem. If that's the case, then you might actually get some performance benefit from the slightly better algorithm (of course, the normal suggestions apply: timeit with real data to know if it's worth the extra lines of code).

نصائح أخرى

Use itertools.product to construct all the pairs and then check all of them

In [27]: import itertools
In [28]: for i, j in itertools.product(a, b):
   ....:     if i+j==12: print i, j
   ....:     
1 11
2 10
3 9
4 8
5 7

This is trivial with list comprehension, and is practically covered on the list comprehension examples for iterating over two lists.

In the first instance, you need to iterate over each list:

[(x,y) for x in a for y in b]

This gives every pair for each list ordered by the elements of a with b (notive the order of the elements below).

[(1, 7), (1, 8), (1, 9), (1, 10), (1, 11), (1, 12), (2, 7), (2, 8),
 (2, 9), (2, 10), (2, 11), (2, 12), (3, 7), (3, 8), (3, 9), (3, 10),
 (3, 11), (3, 12), (4, 7), (4, 8), (4, 9), (4, 10), (4, 11), (4, 12),
 (5, 7), (5, 8), (5, 9), (5, 10), (5, 11), (5, 12), (6, 7), (6, 8),
 (6, 9), (6, 10), (6, 11), (6, 12)
]

Then apply a filter at the end to restrict the list:

>>> a = [1,2,3,4,5,6]
>>> b = [7,8,9,10,11,12]
>>> [(x,y) for x in a for y in b if x+y==12]
[(1, 11), (2, 10), (3, 9), (4, 8), (5, 7)]

This can be created as a generator, when elements are created as needed, instead of creating and storing the whole list in memory if a and b are rather large.

Like so (note the round brackets instead of square brackets):

>>> ((x,y) for x in a for y in b)
<generator object <genexpr> at 0x7f12e1bfef00>

If you have two sets of numbers

a = [1,2,3,4,5,6]
b = [7,8,9,10,11,12]

and can convert your implicit function f(a,b)=0 to an explicit function b=f(a)

Convert at least one of the list of numbers to sets

b = set(b)

and iterate through the other set of numbers and determine if the solution of the equation f(x), lies in the list (set) b

for x in a:
    if f(x) in b:
        print x, f(x)

Example Run

>>> for x in a:
    if f(x) in b:
        print x, f(x)


1 11
2 10
3 9
4 8
5 7

*Analysis of Performance *

  1. zip solution is unsuitable as it does not cater, all combinations of data
  2. product solution is suitable, but you need to evaluate all a * b sets of data
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top