简体   繁体   English

检查一个列表中的项是否存在于另一个列表中

[英]Check if items in one list exists in another list

I have the following data. 我有以下数据。 My goal is to check whether each row is part of the US or not. 我的目标是检查每一行是否属于美国。

data = [', Accomack, Virginia, USA',
 'Elkin, Surry, North Carolina, USA',
 'Philippines',
 '(null)',
 'Texas, United States',
 'Kingston, Washington, Rhode Island, United States']

I first used the following to split them into lists within the list and remove the white space: 我首先使用以下内容将它们拆分为列表中的列表并删除空格:

place = []

for d in data:

   row = d.split(',')

   rowlist = []

   for r in row:

      r_stripped = r.strip()

      rowlist.append(r_stripped)

   place.append(rowlist)

place

I got the following output, which is what I expected: 我得到了以下输出,这是我的预期:

[['', 'Accomack', 'Virginia', 'USA'],
 ['Elkin', 'Surry', 'North Carolina', 'USA'],
 ['Philippines'],
 ['(null)'],
 ['Texas', 'United States'],
 ['Kingston', 'Washington', 'Rhode Island', 'United States']]

Then I used the following to try to see if each item is in the US or not: 然后我使用以下内容尝试查看每个项目是否在美国:

country = []

US = ['USA', 'United States'] 

for p in place:
    for item in US:
        if item in p:
            c = 'US'

        else:
            c = 'Non-US'
    country.append(c)

country

For some reason, the code is not able to capture the first two rows as part of US. 出于某种原因,代码无法捕获前两行作为美国的一部分。

['Non-US', 'Non-US', 'Non-US', 'Non-US', 'US', 'US']

It's even more curious if i remove the 'else: c = 'Non-US' condition, everything becomes 'US'. 如果我删除'else:c ='非美国'条件,一切都变成'美国',这更令人好奇。

Can anyone please tell me what I am not doing right? 任何人都可以告诉我我做得对吗? Thanks! 谢谢!

You should check if any item in p is in the list named US by updating the inner loop like below: 您应该通过更新内部循环来检查p中的任何项是否在名为US的列表中,如下所示:

>>> for p in place:
...     for item in p:
...         if item in US:
...             c = "US"
...             break
...     else:
...         c = "Non-US"
...     country.append(c)

The else clause of the inner for loop is executed only if loop doesn't break . 仅当循环不中断时,才会执行内部for循环的else子句。 When you see item in the US list, you break out of the loop for the next p in the line. 当您在US列表中看到item时,您将跳出该行中下一个p的循环。

-- -

However, you can make it more user-readable by leveraging any() and a little bit of list comprehensions : 但是,您可以通过利用any()和一些列表推导来使其更具用户可读性:

>>> ["US" if any(item in US for item in p) else "Non-US" for p in place]
['US', 'US', 'Non-US', 'Non-US', 'US', 'US']

-- -

any() also helps you eliminate the inner loop totally: any()也可以帮助你完全消除内部循环:

>>> for p in place:
...     if any(item in US for item in p):
...         c = "US"
...     else:
...         c = "Non-US"
...     country.append(c)
new_data = filter(lambda x: 'USA' in x or 'United States' in x, data)
return new_data

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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