On Sep 16, 2013 12:50 PM, "Dhananjay Nene" <[email protected]> wrote: > > On Mon, Sep 16, 2013 at 12:27 PM, Suyash Bhatt <[email protected]> wrote: > > thanks for the response.. > > > > what if i have another list > > > > *e = [*'*my1name1is1','**my2name2is1','xyz','abc']* > > *and in the list d, I want only the elements which are present in e..* > > * > Python 2.7.3 (default, Apr 10 2013, 05:13:16) > [GCC 4.7.2] on linux2 > Type "help", "copyright", "credits" or "license" for more information. > >>> import itertools > >>> > >>> a = ['my1','my2'] > >>> b = ['name1', 'name2'] > >>> c = ['my1', 'my2'] > >>> e = ['my1name1my1','my1name2my1','foobar'] > >>> d = [y for y in (''.join(x) for x in itertools.product(a, b, c)) if y in e] > >>> > >>> d > ['my1name1my1', 'my1name2my1']
Suyash, Again, since you started with itertools, I'm assuming you don't want to have the entire lost evaluated before hand. In which case you're looking for the itertools.ifilter [ http://docs.python.org/2.7/library/itertools.html#itertools.ifilter ] function. Alternatively, you could just change that last statement to be a generator expression instead of a lost comprehension[1]. I suggest you sit down with the itertools docs [ http://docs.python.org/2.7/library/itertools.html ] open in one window, a python console in another; and experiment with the tool set provided to see how best you can express your problem. The module is one of the better documented ones and is reasonably intuitive. Feel free to get back to the list with any specific technical issues you encounter, but, IMHO, us regurgitating the relevant docs here is not the best use of either of our time. That said, this thread is starting to smell of the XY Problem [ http://bit.ly/XyProblem ]. Suyash, it will probably be very helpful to all if you could give us a general idea of what it is that you are trying to achieve with all these tid-bits of information. Maybe could someone could point you to a simple or more appropriate solution. - d [1] unless I'm missing something and list comprehension are inherently lazy. _______________________________________________ BangPypers mailing list [email protected] https://mail.python.org/mailman/listinfo/bangpypers
