简体   繁体   中英

How do I check to see if all of the items in one list are in a second list in python?

I'm new to python and have put this together from a short class on Python and some Googling. I'm trying to compare two lists of strings to see if all of the items of List A are in List B. If any items are not in List B, I want it to print a notification message.

List_A = ["test_1", "test_2", "test_3", "test_4", "test_5"]
List_B = ["test_1", "test_2", "test_3", "test_4"]

Code:

for item in List_A:
     match = any(('[%s]'%item) in b for b in List_B)
     print "%10s %s" % (item, "Exists" if match else "No Match in List B")

Output:

test_1 No Match in List B

test_2 No Match in List B

test_3 No Match in List B

test_4 No Match in List B

test_5 No Match in List B

The first four should match but do not, and the fifth one is correct. I have no idea why it isn't working. Can someone tell me what I'm doing wrong? Any help would be greatly appreciated.

>>> '[%s]'%"test_1"
'[test_1]'

You are checking if "[test_1]" is a substring of some string in list_B , and so forth.

This should work:

for item in List_A:
     match = any(item in b for b in List_B)
     print "%10s %s" % (item, "Exists" if match else "No Match in List B")

But since you are not really looking for substrings, you should test in and nothing more:

for item in List_A:
     match = item in List_B
     print "%10s %s" % (item, "Exists" if match else "No Match in List B")

but you can simply use set difference:

print set(List_A) - set(List_B)

You can convert List_B into a set, as sets provide an O(1) lookup:

Though note that sets allow you to store only hashable(immutable) items. If List_B contains mutable items then convert them to immutable items first, If that is not possible then sort List_B and use bisect module to do binary search on sorted List_B .

List_A = ["test_1", "test_2", "test_3", "test_4", "test_5"]
List_B = ["test_1", "test_2", "test_3", "test_4"]
s = set(List_B)
for item in List_A:
    match = item in s  # check if item is in set s
    print "%10s %s" % (item, "Exists" if match else "No Match in List B")

output:

test_1 Exists
test_2 Exists
test_3 Exists
test_4 Exists
test_5 No Match in List B

Just use a simple for-loop:

List_A = ["test_1", "test_2", "test_3", "test_4", "test_5"]
List_B = ["test_1", "test_2", "test_3", "test_4"]
for i in List_A:
    print "{:>10} {}".format(i, "Exists" if i in List_B else "No Match in List B")
List_A = ["test_1", "test_2", "test_3", "test_4", "test_5"]
List_B = ["test_1", "test_2", "test_3", "test_4"]

for item in List_A:
     match = any((str(item)) in b for b in List_B)
     print "%10s %s" % (item, "Exists" if match else "No Match in List B")

will output

test_1 Exists
test_2 Exists
test_3 Exists
test_4 Exists
test_5 No Match in List B

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