简体   繁体   中英

Python checking if string in list to list

I am having a hard time trying to understand how the "for" function works.

I want to make a script that only outputs the strings in list2 that are not inside list1. For example:

list1 = ["link1.com", "link2.com", "link3.com"]
list2 = ["link2.com", "link123.com"]

for list2 in list1:
    print(list2)

{My intention was that the code printed:

link123.com

But instead it printed the strings from list1}

I can't get it to work. Help would be much appreciated. I am using python 3 by the way.

Use Set for this .

set(list2)-set(list1)

Check with python set

The loop for list2 in list1 is actually an assignment: in each iteration the variable list2 gets the value of the next item in list1 - that is why only the elements of list1 are printed.

You could iterate over the elements of list2 and print, if they are not in list1 :

for element in list2:
    if element not in list1:
        print(element)

For loops

For loops allow you to repeat a piece of code multiple times. You could easily just write a conditional and a print statement multiple times for each item in the list. A for loops allows you to write that piece of code once and have it repeated for each item in the list.

You can iterate over item in list2 by doing the following

for item in list2:
    print(item)

item is an arbitrary variable name that holds the value of the current item we are on, what follows in is the list that we want to iterate over. print(item) is the piece of code we want to repeat for each element in the list.

What this does is goes through every item in list2 and prints them but that is not what we want. We want to check to make sure that item is not in list1. This can be achieved through a conditional statement.

if item not in list1:
    print(item)

Now we can join the two piece of code together.

for item in list2:
    if item not in list1:
        print(item)

Sets

Are a collection of items in no order where every item is unique. These sets are the same ones we encounter in mathematics, therefore we can perform mathematical set operations on them.

To go from a list of items to a set we use sList1 = set(list1) sList1 is now of type set and stores the elements in list1. The same can be done for list2.

Now that we have sList1 and sList2 we want to eliminate any duplicates in the two for that we can take the difference of sList1 and sList2 and print them out as follows print(sList2-sList1) .

We can do all of this in one step. print( set(list2) - set(list1) )

Or if you want to use a for loop (note that this isn't very efficient for large lists):

for string in list2:
    if not string in list1:
        print (string)

The semantic, that python will check if these items are in list1 is NOT part of the for-each-loop.

The 'set' - solution is maybe too advanced for you.

So straightforward you would:

for item in list2:
    if item not in list1:
        print(item) 

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