1
row_names=['a','b','b','b']
col_names=['a','a','a','b','b','b']

How do I only print items that do not match in both? Zip doesnt work due to unequal lengths.

Something along this line?

 for item in row_names, col_names:
    if row_names[item] != col_names[item]:
        print item
jxn
  • 7,685
  • 28
  • 90
  • 172
  • possible duplicate of [Python: zip-like function that pads to longest length?](http://stackoverflow.com/questions/1277278/python-zip-like-function-that-pads-to-longest-length) – Bhargav Rao Jun 12 '15 at 18:30
  • [`izip_longest`](http://docs.python.org/library/itertools.html#itertools.izip_longest) – Bhargav Rao Jun 12 '15 at 18:30
  • 1
    `set(col_names) ^ set(row_names)` – MrAlexBailey Jun 12 '15 at 18:32
  • And a direct answer for your problem [Python list subtraction operation](http://stackoverflow.com/questions/3428536/python-list-subtraction-operation) – Bhargav Rao Jun 12 '15 at 18:34
  • @BhargavRao that only works for strings of unique values? – jxn Jun 12 '15 at 18:40
  • @Jenn Which, `izip_longest`? Nope. It works for everything. In your case you've gotta do `izip_longest(row_names,col_names,fillvalue = 0)` instead of `zip` – Bhargav Rao Jun 12 '15 at 18:43
  • Brother @Jenn I understand your issue. Do go through both the links that I have added. If you still face a problem, please ping me back. I will be more than glad to help you out with an answer. – Bhargav Rao Jun 12 '15 at 18:46

2 Answers2

4

Use set.symmetric_difference:

results = set(col_names).symmetric_difference(set(row_names))
# Or
results = set(col_names) ^ set(row_names)
Tuan Anh Hoang-Vu
  • 1,994
  • 1
  • 21
  • 32
0
row_names=['a','b','b','b']
col_names=['a','a','a','b','b','b']

def diff(row,col):
    big, little = [row,col] if len(row) > len(col) else [col,row]
    map (lambda item: big.pop(big.index(item)) if item in big else None,little) 
    return big

print 'res: {}'.format(diff(row_names,col_names))

res: ['a', 'a']

mitghi
  • 889
  • 7
  • 20