Python compares the same contents in two lists, ignoring case

Compare two lists with the same content, ignoring case.

The general idea is as follows:

First, convert the contents of the list to uppercase or lowercase, and then traverse one of the lists.

To determine whether it is in another list

code

a = ["Apple", "Banana", "Pear", "Peach"]
b = ["apple", "banana", "pear", "grape"]

a = [i.lower() for i in a]
print(a)
b = [i.lower() for i in b]
print(b)
for j in a:
    if j in b:
        print("相同的内容:", j)

Print the result:

In this way, the comparison comes out, but the compared content is not the content in the original list, so it is recommended to convert it to upper and lower case and suggest one of them.

If the content in a wants to be original

a = ["Apple", "Banana", "Pear", "Peach"]
b = ["apple", "banana", "pear", "grape"]

for j in a:
    if j.lower() in [i.lower() for i in b]:
        print("相同的内容:", j)

Print the result:

Guess you like

Origin blog.csdn.net/qq_33210042/article/details/133322168