0

Hello there i've written a small functions that takes two lists and compares them for duplicating pairs, and returns a boolean value.

For instance ([1,2,3],[2,1,4]) returns false and ([1,2,3],[3,4,5]) returns true But i would like the argument to take any given amount of lists, instead of just two.

Here's my program so far:

def check(xrr, yrr):
    x = xrr[:]
    x.sort()
    y = yrr[:]
    y.sort()
    for i in range(len(x)-1):
        if x[i]==y[i]:
            return False
    return True

But also it isnt exactly working correctly yet, as ([1,2,3],[1,4,5]) also returns false.

Any hints and ideas is highly appreciated

9
  • 1
    What exactly do you mean by "duplicating pairs"? Do you want to return True if there is any number that can be found in both lists? Or does it have to be in the same position in both lists? Or just what? Commented Dec 7, 2010 at 7:59
  • 1
    Why would you expect ([1,2,3],[1,4,5]) to return False? The first element in both after sorting is 1, so x[0]==y[0], and it should return false. The question is, what do you mean by duplicating pairs? Commented Dec 7, 2010 at 8:00
  • @ A A, yeah youre excactly right, i noticed that as well, that's about to change! Commented Dec 7, 2010 at 8:01
  • 1
    What is the logic behind - [1,2,3],[2,1,4] returns False. And [1,2,3],[3,4,5] returns True? Commented Dec 7, 2010 at 8:02
  • about duplicating pairs, i mean that for instance [1,2,3],[2,1,4] has a duplicate pair: [1,2] but [1,2,3],[1,4,5] doesnt. I'll work some more on it :) Commented Dec 7, 2010 at 8:03

3 Answers 3

3
import itertools

def check(*args):
  r = None
  for l in args:
    s = set(frozenset(x) for x in itertools.combinations(l, 2))
    if r is None:
      r = s
    else:
      r &= s
    if not r:
      return True
  return False

print check([1, 2, 3], [3, 4, 5])
print check([1, 2, 3], [2, 1, 4])
Sign up to request clarification or add additional context in comments.

1 Comment

+1. More robust, with interesting application of itertools.
3
def dupLists(List0,*Lists):
    result=set(List0)
    for l in Lists:
        result=result.intersection(l)
        if len(result)<2:
            return True
    return False

5 Comments

+1 for the right approach, but you appear to have the return values the wrong way around.
the return values seem right to me? However this only works on two lists, instead of any amount of lists
@user457142: It does works on any amount of lists(at least two). that's why I wrote it for.
but dupLists([1,2,3],[2,1,4]) returns false, which is correct, however dupLists([1,2,3],[2,1,4],[3,5,6]) returns true, which is not correct
Aha, that's because I misunderstood. This function return False only all the lists duplicated.
1

As a naive implementation, you can define a list of lists and hash the internal representation. For instance:

def check(given):
   hash = {}
   for li in given:
      for val in li:
         if val in hash:
            return False
         hash[val] = 1
   return True

This works if your input data sets contain a small number of unique elements relative to memory. If you expect to receive extremely large data sets, you might need to consider a more elaborate approach.

Also notable is this will return False for repeating elements within the same data set, and for repeating values at any location within the data set. This is trivial to refactor, or to rewrite entirely as the other solutions listed here.

5 Comments

Thanks alot, this is more or less excactly what i was looking for :)
has_key is totally obsolete and deprecated. Use the in keyword. instead. This has the advantage of being much faster in Python 2 and available in Python 3.
@aaronasterling +1 for the reminder. Deprecated usage of has_key is a dirty habit carried forward from 2.3. I'll also point out that stackoverflow.com/questions/4374426/list-checking-in-python/… is the more robust, more correct solution.
This is wrong isn't it? It will return False for any duplicate.
@aaronasterling Which is precisely what I call out. If a stronger or more correct answer is preferred, I'd be quite happy to delete this one. Assuming someone else doesn't, that is.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.