简体   繁体   中英

How to check two lists against each other?

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 ie [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.

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

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM