1

I need to check if list with lists contains list with similar values to the specified list, values may be in different order but in case all values are same it should return true

a= ["1","2","3","4","5"]

b= ["2","3","6","4","7"]

e = (["1","3","2","4","5"],["2","3","6","4","7"])


CombinationFound = []
for i in e:
    if i == a:
        CombinationFound = True
        break;
    else:
        CombinationFound = False

it should return true since ["1","2","3","4","5"] and ["1","3","2","4","5"] have same values

Dariusz Woźniak
  • 9,640
  • 6
  • 60
  • 73

5 Answers5

0

Try to convert those lists into sets:

def isthesame(a,b):
    return set(b) == set(a)

For example if you have:

a= ["1","2","3","4","5"]
b= ["2","3","6","4","7"]

This solution should work for lists that don't have duplicit items.

nio
  • 5,141
  • 2
  • 24
  • 35
  • 1
    `isthesame("cat", "cart")` returns `True`. Is that intentional? – Kevin Nov 25 '13 at 19:11
  • @Kevin You didn't compare two lists but two strings. Try to use `isthesame(["cat"],["cart"])` – nio Nov 25 '13 at 19:17
  • 1
    How about `isthesame([1,2,3], [1,2,3,4,5])`, which also returns `True`? – Kevin Nov 25 '13 at 19:17
  • 1
    The point is that set difference never goes "negative". Non-equal sets can have a zero length difference if the left hand set is a subset of the right hand set. Use the equality operator to see if two sets are equal. – Peter DeGlopper Nov 25 '13 at 19:19
  • You're right, the set substraction is position dependent so there's need to check both substractions. – nio Nov 25 '13 at 19:21
  • No, there's no need to check both subtractions. Just use `==`. That's what it's for for sets. – Peter DeGlopper Nov 25 '13 at 19:22
0

You need to convert the list you're trying to match against into something you can compare against without caring about the original order. If the number of appearances of an element of the list counts, use sorted lists. If it does not, use sets.

list_to_match = sorted(a)
combination_found = False
for i in e:
    if sorted(i) == list_to_match:
        combination_found = True
        break

That version will distinguish lists with different numbers of repeated elements - that is, [0, 1, 1, 2] will not match [0, 1, 1, 1, 2]. If you don't care about that possibility, use set(a) and set(i) instead:

set_to_match = set(a)
combination_found = False
for i in e:
   if set(i) == set_to_match:
       combination_found = True
       break

Or, for a more concise version, use the built-in any function with a generator expression:

list_to_match = sorted(a)
combination_found = any(sorted(i) == list_to_match for i in e)
Peter DeGlopper
  • 36,326
  • 7
  • 90
  • 83
0

Python sets are a much better implementation for your particular problem.

Sets are mathematical objects that contain data, but have methods to determine unions, intersections, differences etc between two collections.

Using:

set(a) == set(b)

should give you the result you want. As long as by 'similar' you mean 'the same'.

hyleaus
  • 755
  • 1
  • 8
  • 21
0

Different way to compare without the for loop:

found = any(set(a)==set(l) for l in e)
Bryan
  • 17,112
  • 7
  • 57
  • 80
0
def checker(list_of_lists, example):
    for i in list_of_lists:
        if tuple(sorted(i)) == tuple(sorted(example)):
            return True
    return False
pod2metra
  • 256
  • 1
  • 6