2

If I have two lists I want to be able to see if the items in one list compare to a different list. For example if I had list1=[1,2,3] and list2=[2,3,5,1] I want to be able to see if the numbers in list 2 match list 1 without them being in the same order and even if there are also other numbers. Sort of like in math when you had two sets of numbers and had to get the intersection of them. Is there a way to see if list2 has all the items that are in list1 regardless of order or other numbers? I'm using it for a if command to detect if something is true based on whether the list is equivalent to the other list and then change the variable to "true".

Here's an example of something similar to the bit of code I'm trying to get to work.

listOne=[]
listRight=[1,2,5]

right="false"
while(win != "true"):

  option=input("What number would you like to add to list one?")
    if(option=="1"):
        listOne.append(1)
    elif(option=="2"):
        listOne.append(2)

  if(listOne==listRight):
    right="true"

Thanks for the help.

Note: There won't be any duplicates in my lists. One will be a list of 3 numbers i.e. [1,4,7] the other list will be anywhere from zero to nine numbers only used the numbers 1-9. I want to be able to check if all 3 of the the numbers are anywhere in the second list regardless if there's extra numbers. Like if [1,5,9] was the first list and [7,1,3,6,9,5] was the second list it would come back true that they equal each other.

JakeNBake
  • 69
  • 5
  • Possible duplicate of [Determine if 2 lists have the same elements, regardless of order?](http://stackoverflow.com/questions/8866652/determine-if-2-lists-have-the-same-elements-regardless-of-order) – Arya McCarthy Apr 27 '17 at 02:22

3 Answers3

5

Yes, use sets:

>>> list1=[1,2,3]
>>> list2=[2,3,5,1]
>>> set(list1) & set(list2) # intersection
{1, 2, 3}
>>> set(list1) | set(list2) # union
{1, 2, 3, 5}
>>> set(list1) - set(list2) # set difference
set()
>>> set(list2) - set(list1) # set difference
{5}
>>> set(list1) ^ set(list2) # symmetric difference
{5}
>>>

And subset relations:

>>> set(list1) < set(list1) # proper subset with <
False
>>> set(list1) < set(list1)
False
>>> set(list1) < set(list2)
True
>>> set(list1) <= set(list1) # normal subset
True
>>>
juanpa.arrivillaga
  • 88,713
  • 10
  • 131
  • 172
3

Python has a set type, and you can use a <= b (or the less readable b.issubset(a)) to check whether a is a subset of b.

Some examples ({a, b, c} is shorthand for set([a, b, c])):

>>> {1, 2} <= {1, 2, 3}
True

>>> {2, 1, 5} <= {1, 5, 2}
True

>>> set() <= {0}
True

>>> {1, 2, 4} <= {1, 2, 5}
False

Used in your code:

attempt = set()
right = {1, 2, 5}

while not right <= attempt:
    option = input("What number would you like to add to list one?")
    attempt.add(int(option))
Ry-
  • 218,210
  • 55
  • 464
  • 476
  • Thanks I haven't tried it yet but will this also work even if there's other numbers? Basically this is for a tic tac toe and one list is the way you can win and the other list is the places they've gone (I know this isn't a good way to do it) but if I had a list of [1,2,3] and I wanted to see if those 3 numbers are in the list [2,6,9,3,4,1] will doing <= the sets work? – JakeNBake Apr 27 '17 at 02:37
  • @JacobGilger: Yes. It’s an “is subset of” operator. – Ry- Apr 27 '17 at 03:53
0

What exactly do you mean when you say you want to see if the numbers in the two lists match? Suppose list1 = [1, 1] and list2 = [1, 1, 1], would you expect a return value of True in this case? If yes, or you don't care, then simply using sets is your answer.

list1 = [1, 2, 3]
list2 = [1, 3, 3, 3, 2]
list3 = [3, 2]
print(set(list1) == set(list2)) # => True
print(set(list1) == set(list3)) # => False

I'm guessing that in your application, you don't expect to encounter duplicates, so this should be acceptable. However, if you do expect duplicates and want to retain them, you probably want to construct a method from scratch you can just sort the lists firsts (d'oh).

Apollys supports Monica
  • 2,938
  • 1
  • 23
  • 33