Re: [BangPypers] need help w.r.t itertools
On Mon, Sep 16, 2013 at 12:27 PM, Suyash Bhatt bhatt.suy...@gmail.com 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'] ___ BangPypers mailing list BangPypers@python.org https://mail.python.org/mailman/listinfo/bangpypers
Re: [BangPypers] need help w.r.t itertools
On Sep 16, 2013 12:50 PM, Dhananjay Nene dhananjay.n...@gmail.com wrote: On Mon, Sep 16, 2013 at 12:27 PM, Suyash Bhatt bhatt.suy...@gmail.com 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 BangPypers@python.org https://mail.python.org/mailman/listinfo/bangpypers
[BangPypers] need help w.r.t itertools
Hi all, i need help in finding the most optimized way of making a list using itertools. Suppose I have 3 lists: *a=[‘my1’,’my2’] * ** *b=[‘name1’,’name2’]* *c=[‘is1’]* I want to iter through all and form another list with the following appended strings: *d=['my1name1is1','my1name2is1','my2name1is1','my2name2is1']* Can this be done in a single line using the itertools module?? Regards, Suyash ___ BangPypers mailing list BangPypers@python.org https://mail.python.org/mailman/listinfo/bangpypers
Re: [BangPypers] need help w.r.t itertools
d = [''.join(x) for x in itertools.product(a, b, c)] -- Dhruv Baldawa (http://www.dhruvb.com) On Fri, Sep 13, 2013 at 4:53 PM, Suyash Bhatt bhatt.suy...@gmail.comwrote: Hi all, i need help in finding the most optimized way of making a list using itertools. Suppose I have 3 lists: *a=[‘my1’,’my2’] * ** *b=[‘name1’,’name2’]* *c=[‘is1’]* I want to iter through all and form another list with the following appended strings: *d=['my1name1is1','my1name2is1','my2name1is1','my2name2is1']* Can this be done in a single line using the itertools module?? Regards, Suyash ___ BangPypers mailing list BangPypers@python.org https://mail.python.org/mailman/listinfo/bangpypers ___ BangPypers mailing list BangPypers@python.org https://mail.python.org/mailman/listinfo/bangpypers
Re: [BangPypers] need help w.r.t itertools
On Fri, Sep 13, 2013 at 5:01 PM, Dhruv Baldawa dhruvbald...@gmail.comwrote: d = [''.join(x) for x in itertools.product(a, b, c)] Actually, using itertools.imap would ensure that the elements aren't computed till necessary. So... d = itertools.imap(''.join, itertools.product(a, b, c)) - d ___ BangPypers mailing list BangPypers@python.org https://mail.python.org/mailman/listinfo/bangpypers