0

How can I compare two multidimensional lists in python 2.6.6 and show the different elements.

I am trying to compare multidimensional list A with multidimensional list B and output the elements that are present in list A but not present in list B

aArray=[]
a1=[1],[2],[3]
a2=[1],[4],[5]

bArray=[]
b1=[1],[2],[3]
b2=[1],[6],[7]

aArray.append(a1)
aArray.append(a2)
aArray
[([1], [2], [3]), ([1], [4], [5])]

bArray.append(b1)
bArray.append(b2)
bArray
[([1], [2], [3]), ([1], [6], [7])]

aArray.difference(bArray)

Expected result:

([1],[4],[5])

actual result:

aArray.difference(bArray)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: 'list' object has no attribute 'difference'
snakecharmerb
  • 47,570
  • 11
  • 100
  • 153

2 Answers2

0

I think a simple list comprehension will do:

a = [([1], [2], [3]), ([1], [4], [5])]
b = [([1], [2], [3]), ([1], [6], [7])]

print([x for x in a if x not in b])
[([1], [4], [5])]
YOLO
  • 20,181
  • 5
  • 20
  • 40
0

First of all .difference is a build in method for sets and does will not work for lists.

Hence, AttributeError: 'list' object has no attribute 'difference'

Just use of forloop to read the elemets in aArray and using if to print the

elements in aArary and not bArray using

[diff for diff in aArray if diff not in bArray]

Bane
  • 484
  • 3
  • 13