1

Giving a list, how can I select element pairs that satisfy some criterion? I know a linear search algorithm can achieve this:

b = []
for i in range(len(a)-1):
    for j in range(i+1,len(a)):
        if isTrue(a[i],a[j]):
            b.append([a[i],a[j]])

Any better solution can do this more efficiency?

Update

@scytale's comment inspired me a solution. But it can not be perfect.

For example, a = [1.2,3.1,0.3,4.2,5.6,2.7,1.1]. I want to get pairs of elements that the sum of which is less than 3.

b = [(x,y) for x in a for y in a if (x+y)<3 and x!=y]

This will give duplicate pairs of:

[(1.2,0.3),(1.2,1.1),(0.3,1.2),(0.3,1.1),(1.1,1.2),(1.1,0.3)]

But what I want is:

[(1.2,0.3),(1.2,1.1),(0.3,1.1)]
xibinke
  • 865
  • 2
  • 9
  • 11
  • possible duplicate of [Double Iteration in List Comprehension](http://stackoverflow.com/questions/2522503/advanced-python-list-comprehension) – scytale Aug 25 '15 at 14:29
  • As I updated, I can not get non-repetitive result... – xibinke Aug 25 '15 at 14:52
  • 1
    please use search: http://stackoverflow.com/questions/7961363/python-removing-duplicates-in-lists – scytale Aug 25 '15 at 14:59

1 Answers1

4

What about using combinations and filter?

from itertools import combinations

c = combinations(a, 2)
f = filter(lambda x, y: isTrue(x, y), c)

Or using list comprehension:

result = [(x, y) for x, y in c if isTrue(x, y)]
Delgan
  • 18,571
  • 11
  • 90
  • 141
  • The last script is great. As for the filter, why it raises error of "lambda takes exactly two arguments..." when using "f = filter(lambda x,y: x + y < 3,c)"? – xibinke Aug 25 '15 at 15:03