我有两个数组,每个数组都有多个子列表,每个子列表有3个项目。我试图将这两个列表的子列表作为单个实体进行比较,而不是将List1的子列表中的项目与List2的子列表中的项目进行检查。 这些值是使用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')
答案 0 :(得分: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
答案 1 :(得分:0)
如果您的列表采用以下格式:
git merge
刚刚:
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]
答案 2 :(得分: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']
答案 3 :(得分: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
答案 4 :(得分: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')