简体   繁体   English

如何检查列表中的所有元素是否都包含在其他元素中

[英]How to check if all elements of a list are contained in other one

I've two list and need to check if all elements of list_1 are contained in list_2 id not, save those elements in a new list. 我有两个列表,需要检查list_1的所有元素是否都包含在list_2 id中,请将这些元素保存在新列表中。

SO I've this: 所以我有这个:

list_1 = ['item','item','item']
list_2 = ['item_2','item_2','item_2']
list_3 = []

for i in range(len(list_1)):
    flag = True
    aux = list_1[i]
    for j in range(len(list_2)):
        if aux == list_2[j]:
            flag == False
            break
    if flag:
        list_3.append(aux)

But this is very slow, there is a way to improve the speed? 但这很慢,有没有提高速度的方法?

maybe some built pandas function? 也许一些内置的熊猫功能?

Edit. 编辑。

I don't need pandas to this, but the list are infact columns two Data Frames, I just write it with list because it's a more general case. 我不需要大熊猫,但是列表是两个数据框的实际列,我只用list来写它,因为这是更一般的情况。

IIUC I think you could use isin and all methods of pd.Series : IIUC我想你可以使用isinall的方法pd.Series

import pandas as pd
l1, l2 = map(pd.Series, [list_1, list_2])

In [3]: l1      
Out[3]:         
0    item       
1    item       
2    item       
dtype: object   

In [4]: l2      
Out[4]:         
0    item_2     
1    item_2     
2    item_2     
dtype: object   

In [5]: l1.isin(l2)
Out[5]:
0    False
1    False
2    False
dtype: bool

In [6]: l1.isin(l2).all()
Out[6]: False

You seem to be interested in the union and difference of sets . 您似乎对集合的和差异感兴趣。

You could check for these, like this: 您可以检查这些,如下所示:

a = set([1, 5, 6, 0])
b = set([0, 8, 2, 3, -5])
a.difference(b)   # returns: {1, 5, 6}
a.intersection(b) # returns {0}
b.difference(a)   # returns: {-5, 2, 3, 8}

如果要在list_1中找到不在list_2的元素并将其保存在其他列表中(在本例中为list_3 ,则可以使用列表 list_3

>>> list_3 = [i for i in list_1 if i not in list_2]

I find numba to be fun for loops, it really brings up the speed with minimum effort: 我发现numba对于循环很有趣,它确实以最少的努力提高了速度:

from numba import jit
@jit
def jitstack():
    list_1 = ['item','item','item']
    list_2 = ['item_2','item_2','item_2']
    list_3 = []
    for i in range(len(list_1)):
        flag = True
        aux = list_1[i]
        for j in range(len(list_2)):
            if aux == list_2[j]:
                flag == False
                break
        if flag:
            list_3.append(aux)

Timed with timeit in iPython notebook: https://drive.google.com/file/d/0B0KNIF4xMP3UNW93LWlmWUFqbnc/view?usp=sharing 在iPython笔记本中与timeit一起计时: https ://drive.google.com/file/d/0B0KNIF4xMP3UNW93LWlmWUFqbnc/view ? usp = sharing

Original: 3.72 µs per loop 原始值:每个循环3.72 µs

Numba: 22.4 ns per loop Numba:每个循环22.4 ns

and also the list_3 = [i for i in list_1 if i not in list_2]: 1.22 µs per loop 并且list_3 = [如果i不在list_2中,则i在list_1中为i]:每个循环1.22 µs

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

相关问题 如何检查列表中的元素是否包含在其他列表中? - How do i check if the elements in a list are contained in other list? 检查第二个列表中包含一个列表的元素数量,包括重复项 - Check how many elements of one list are contained in a second list, including duplicates 如何查找列表元素是否包含在没有完全匹配的其他列表中 - how to find if list elements are contained in other list without exact match 如何检查列表python中是否包含任何列表元素 - How to check if any of a list elements is contained in a list python 如何获取包含在另外两个列表中的元素的列表? - How to get a list with elements that are contained in two other lists? 检查一个列表的元素是否在另一个子列表的元素中 - Check if elements of one list are in elements of other sub-list 如何获取未包含在另一个列表中的n个元素? - How to get n elements of a list not contained in another one? 如何检查列表中包含的类型? - How to check the types contained in a list? 检查一个列表的每个元素是否是另一个列表的所有元素的倍数 - Check if each element of one list is a multiple of all the elements of another list 如何在 Python 中轻松检查一个列表中的所有项目都包含在另一个列表中? - How can I easily check in Python that all items in one list are included in the other list?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM