如何将两个列表的子列表作为单个实体进行比较?

问题描述 投票:0回答:5

我有两个数组,每个数组都有多个子列表,每个子列表包含3个项目。我试图将这两个列表的子列表作为单个实体进行比较,而不是使用List2子列表中的项目检查List1子列表中的项目。使用df.vaues从excel表中提取这些值

我尝试使用传统的python列表函数并使用嵌套的for循环

arr1:  [['AU002' '000000000037080' 'VB_ADJ']  ['AU002' '000000000037080' 'VB_ADJ']  ['AU002' '000000000039325' 'VB_ADJ']  ['AU002' '000000000039325' 'VB_ADJ']]

arr2:  [['AU002' '000000000037080' 'HUNTER_DOUGLAS']  ['AU002' '000000000037080' 'EXP'] ['AU002' '000000000037080' 'GEN']  ['AU002' '000000000037080' 'VB_ADJ']  ['AU002' '000000000039325' 'EXP']]

这里arr1的元素4不存在于arr2中,但是我使用的方法无法得到正确的输出

#1.
mask = np.isin (arr1, arr2)
mask

#2.
i=0
for each in arr1:
   j=0
   if(i<3):
       for every in arr2:
           if(j<3):
               if(each[i]==every[j]):
                   print("found",each[i])
            else :
                print("not found",each[i])
        j+=1
i+=1

#3.
for each in arr1:
    for every in arr2:
        if each==every:
            print('found')
        else:
            print('not found')
#4.
result =  all(elem in arr2  for elem in arr1)
if result:
    print('Found')
else:
    print('Not Found')
python pandas numpy
5个回答
0
投票

numpy的array_equal在这里可以很好地工作。你可以在这里阅读更多相关信息:https://docs.scipy.org/doc/numpy/reference/generated/numpy.array_equal.html

import numpy as np

lis1= [['AU002', '000000000037080', 'VB_ADJ'],  ['AU002', '000000000037080' ,'VB_ADJ'] , ['AU002' ,'000000000039325', 'VB_ADJ'],  ['AU002', '000000000039325', 'VB_ADJ']]
lis2=[['AU002', '000000000037080', 'HUNTER_DOUGLAS'] , ['AU002', '000000000037080', 'EXP'] ,['AU002', '000000000037080' ,'GEN'],  ['AU002' ,'000000000037080' ,'VB_ADJ'] , ['AU002' ,'000000000039325', 'EXP']]

for i in lis1:
    lis=[]
    for j in lis2:
        lis.append(np.array_equal(i,j))
    if True in lis:
        print('Found ', i)
    else:
        print('Not Found ', i)

产量

Found  ['AU002', '000000000037080', 'VB_ADJ']
Found  ['AU002', '000000000037080', 'VB_ADJ']
Not Found  ['AU002', '000000000039325', 'VB_ADJ']
Not Found  ['AU002', '000000000039325', 'VB_ADJ']

0
投票

你可以用这个:

found_list = [elem in arr2 for elem in arr1]

测试出来:

arr1 = [['AU002', '000000000037080', 'VB_ADJ'],
        ['AU002', '000000000037080', 'VB_ADJ'],
        ['AU002', '000000000039325', 'VB_ADJ'],
        ['AU002', '000000000039325', 'VB_ADJ']]
arr2 = [['AU002', '000000000037080', 'HUNTER_DOUGLAS'],
        ['AU002', '000000000037080', 'EXP'],
        ['AU002', '000000000037080', 'GEN'],
        ['AU002', '000000000037080', 'VB_ADJ'],
        ['AU002', '000000000039325', 'EXP']]

found_list = [elem in arr2 for elem in arr1]
print (found_list)

输出:

[True, True, False, False] # Only 0-th and 1-st elems of arr1 are in arr2

0
投票

如果您的列表采用以下格式:

a1 = [['AU002' ,'000000000037080' ,'VB_ADJ'],  ['AU002', '000000000037080' ,'VB_ADJ'],  ['AU002', '000000000039325' ,'VB_ADJ'],  ['AU002', '000000000039325' ,'VB_ADJ']]

 b1 = [['AU002' ,'000000000037080' ,'HUNTER_DOUGLAS'] , ['AU002', '000000000037080', 'EXP'] ,['AU002', '000000000037080' ,'GEN'] , ['AU002' ,'000000000037080' ,'VB_ADJ'] , ['AU002', '000000000039325', 'EXP']]

只是:

[sublist_a1 for sublist_a1 in a1 for sublist_b1 in b1 if sublist_a1 == sublist_b1]

提供两个列表中的子列表:

[['AU002', '000000000037080', 'VB_ADJ'],
 ['AU002', '000000000037080', 'VB_ADJ']]

0
投票

以下是有用的片段

from pandas import DataFrame

arr1 = [['AU002', '000000000037080', 'VB_ADJ'],
        ['AU002', '000000000037080', 'VB_ADJ'],
        ['AU002', '000000000039325', 'VB_ADJ'],
        ['AU002', '000000000039325', 'VB_ADJ']]
arr2 = [['AU002', '000000000037080', 'HUNTER_DOUGLAS'],
        ['AU002', '000000000037080', 'EXP'],
        ['AU002', '000000000037080', 'GEN'],
        ['AU002', '000000000037080', 'VB_ADJ'],
        ['AU002', '000000000039325', 'EXP']]

df1 = DataFrame.from_records(arr1)
df1.columns = ["Col1", "Col2", "Col3"]

df2 = DataFrame.from_records(arr2)
df2.columns = ["Col1", "Col2", "Col3"]

df1['compressed']=df1.apply(lambda x:'%s%s%s' % (x['Col1'],x['Col2'],x['Col3']),axis=1)
df2['compressed']=df2.apply(lambda x:'%s%s%s' % (x['Col1'],x['Col2'],x['Col3']),axis=1)
df1['Success'] = df1['compressed'].isin(df2['compressed']).astype(int)

print(df1)

输出:

    Col1             Col2    Col3                  compressed  Success
0  AU002  000000000037080  VB_ADJ  AU002000000000037080VB_ADJ        1
1  AU002  000000000037080  VB_ADJ  AU002000000000037080VB_ADJ        1
2  AU002  000000000039325  VB_ADJ  AU002000000000039325VB_ADJ        0
3  AU002  000000000039325  VB_ADJ  AU002000000000039325VB_ADJ        0

0
投票

我尝试了以下代码:对我来说工作得很好。谢谢大家的答案。最好的祝福。

由于cmp()函数在python 3.x中不可用

def cmp(a, b):
    if((a>b) - (a<b) ==0):
        return True
    else:
        pass
flag=0
for i in invoice_temp2:
    for j in val_query2:
        if(cmp(i,i)):
            flag=0
            break
        else:
            flag=1
            continue

if flag==1:
    print('Not Found')
else:
    print('Found')
© www.soinside.com 2019 - 2024. All rights reserved.