1

i have two lists:

mylist1=[['a','1'],['a',''],['a','1'],['a','3'],['b','2'],['c','2'],['c','3'],['c',''],['c','2']] 

and

mylist2=[['a','1','lemon'],['a','2','coconut'],['a','3','chocolate'],['a','4','watermelon'],['b','1','mango'],['b','2','apple'],['b','3','tree'],['b','4','apple'],['c','1','water'],['c','2','fire'],['c','3','mountain']]

I want to find out those lists from "mylist2" whose zero and 1st position items are not in "mylist1",like;

result=[['a','2','coconut'],['a','4','watermelon'],['b','1','mango'],['b','3','tree'],['b','4','apple'],['c','1','water']]

Also in above result consider lists mylist1[1]=['a',''] and mylist1[7]=['c',''] (has null in their 1st position) and 1st position is not matched with any list from mylist2.

is there any solution???

namit
  • 6,780
  • 4
  • 35
  • 41

2 Answers2

3

The following should work for you:

[x for x in mylist2 if x[:2] not in mylist1]
Abel
  • 56,041
  • 24
  • 146
  • 247
Acorn
  • 49,061
  • 27
  • 133
  • 172
  • 2
    Use `foo not in bar` instead of `not foo in bar`. It's more readable. – Avaris Mar 30 '12 at 05:44
  • @Avaris, ah, quite right. And of course there's a stackoverflow question on the issue too :) http://stackoverflow.com/questions/3481554/if-loop-x-not-in-vs-not-x-in – Acorn Mar 30 '12 at 05:46
  • 1
    this uses O(N*M) operations (N,M = length of each list). If N*M >10**6 you will start to see a big slow down in performance, in which case I would recommend you change "x[:2] not in mylist1" to "tuple(x[:2]) not in set(map(tuple,mylist1))" – Rusty Rob Mar 30 '12 at 06:12
  • @robert king: yes my lists are too big!!! their N*M is at-least four times bigger than 10**6. – namit Mar 30 '12 at 06:31
  • Then I suggest you use sets to speed up lookup times. – Rusty Rob Mar 30 '12 at 06:38
0

Finding overlapping / non-overlapping sets is easy by switching the list objects into set objects and using one of their methods for comparing the sets.

To get there, you might need to chop off the "data" part (the label string - chocolate etc.).

There are many ways to approach this. This is one example:

>>> aa = [[1,'a'],[2,'b'],[3,'c'],[4,'d'],[5,'e']]
>>> ak = dict(aa)
>>> ak
{1: 'a', 2: 'b', 3: 'c', 4: 'd', 5: 'e'}
>>> ak.keys()
[1, 2, 3, 4, 5]
>>> a = set(ak.keys())
>>> b = set([4,5,6,7,8])
>>> a.difference(b)
set([1, 2, 3])
>>> ra = list(a.difference(b))
>>> aa = [[1,'a'],[2,'b'],[3,'c'],[4,'d'],[5,'e']]
>>> aa
[[1, 'a'], [2, 'b'], [3, 'c']]
ddotsenko
  • 4,926
  • 25
  • 24