-1

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!

user3210986
  • 211
  • 2
  • 9
  • 1
    This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself. –  Jan 21 '14 at 02:50
  • @LegoStormtroopr: You can't figure this problem out, from the information provided? – Robert Harvey Jan 21 '14 at 02:53
  • 3
    Do you only care about equally-indexed elements (i.e. 1+7; 2+8; 3+9...) or every combination of the two lists? Your question is unclear. – roippi Jan 21 '14 at 02:54
  • @RobertHarvey A lazy question deserves a lazy comment. No code was included, no attempt at solving the problem was shown. –  Jan 21 '14 at 03:32
  • @LegoStormtroopr: Then say that, not some faux proxy for lazy. – Robert Harvey Jan 21 '14 at 03:57
  • @RobertHarvey Given that there are a few answers here that now don't answer the OPs question (changed from pairs to all permutations), I think its fair to say the original question *did* lack the information to diagnose the problem. Now that the asker has added additional information solutions have cropped up to answer what they needed. If they had added code initially, it might have been more obvious what they were attempting to do. –  Jan 21 '14 at 03:59
  • possible duplicate of [Combining 2 lists in python](http://stackoverflow.com/questions/1673005/combining-2-lists-in-python) – zmo Mar 07 '14 at 00:04

4 Answers4

2

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).

mgilson
  • 300,191
  • 65
  • 633
  • 696
  • This only compares paired combinations, not every combination as the edit requests. –  Jan 21 '14 at 03:33
  • @LegoStormtroopr -- I guess that's what I get for posting and then walking away ;-). I've updated with some options for the OP's edited request. – mgilson Jan 21 '14 at 04:56
1

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
waitingkuo
  • 89,478
  • 28
  • 112
  • 118
  • `itertools` is over kill for this when it can be done using simple list comprehensions. –  Jan 21 '14 at 03:40
  • 1
    @LegoStormtroopr -- disagree. every `itertools.product` could be broken down into simple list comprehensions (although possibly deeply nested). Generally speaking though, I prefer `itertools.product` simply because I can almost never remember the order of nested list-comprehensions (it's counter intuitive to me) -- And I don't think I'm the only one who shy's away from nested comprehensions for that reason ... – mgilson Jan 21 '14 at 04:58
1

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>
0

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
Abhijit
  • 62,056
  • 18
  • 131
  • 204