Re: [Tutor] Files Merging
On 10/11/2012 07:13 AM, Sunil Tech wrote: Hi all, Greetings to you... it been so helpful for me to go through your all mails support i wish it still continues. I have two text files. text1 contains This is from Text1 --- 1st line This is from Text1 --- 2nd line This is from Text1 --- 3rd line This is from Text1 --- 4th line This is from Text1 --- 5th line text2 contains This is from Text2 --- 1st line This is from Text2 --- 2nd line This is from Text2 --- 3rd line This is from Text2 --- 4th line This is from Text2 --- 5th line i want result in text3 like This is from Text1 --- 1st line This is from Text2 --- 1st line This is from Text1 --- 2nd line This is from Text2 --- 2nd line This is from Text1 --- 3rd line This is from Text2 --- 3rd line This is from Text1 --- 4th line This is from Text2 --- 4th line This is from Text1 --- 5th line This is from Text2 --- 5th line but condition is should not use any loops waiting for your reply, thank you in advance. Regards, Sunil G. What are the other constraints on this homework assignment? Are list comprehensions permitted? Seems likely you can do it readily with a list comprehension using zip(). One line, total. -- DaveA ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Files Merging
i used zip(), but it gives me result in list of tuples format. But i don't get in a exact expect format (as mentioned) no loopings are allowed. On Thu, Oct 11, 2012 at 5:30 PM, Dave Angel d...@davea.name wrote: On 10/11/2012 07:13 AM, Sunil Tech wrote: Hi all, Greetings to you... it been so helpful for me to go through your all mails support i wish it still continues. I have two text files. text1 contains This is from Text1 --- 1st line This is from Text1 --- 2nd line This is from Text1 --- 3rd line This is from Text1 --- 4th line This is from Text1 --- 5th line text2 contains This is from Text2 --- 1st line This is from Text2 --- 2nd line This is from Text2 --- 3rd line This is from Text2 --- 4th line This is from Text2 --- 5th line i want result in text3 like This is from Text1 --- 1st line This is from Text2 --- 1st line This is from Text1 --- 2nd line This is from Text2 --- 2nd line This is from Text1 --- 3rd line This is from Text2 --- 3rd line This is from Text1 --- 4th line This is from Text2 --- 4th line This is from Text1 --- 5th line This is from Text2 --- 5th line but condition is should not use any loops waiting for your reply, thank you in advance. Regards, Sunil G. What are the other constraints on this homework assignment? Are list comprehensions permitted? Seems likely you can do it readily with a list comprehension using zip(). One line, total. -- DaveA ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Files Merging
On Thu, Oct 11, 2012 at 7:13 AM, Sunil Tech sunil.tech...@gmail.com wrote: text1 contains This is from Text1 --- 1st line text2 contains This is from Text2 --- 1st line i want result in text3 like This is from Text1 --- 1st line This is from Text2 --- 1st line but condition is should not use any loops Use itertools.zip_longest() and itertools.chain.from_iterable(), along with the text3.writelines(). zip_longest allows the files to be of different lengths. zip would terminate at the shortest number of lines, but fillvalue of zip_longest supplies a default value (set to an empty string). chain.from_iterable joins the tuples from zip_longest as one iterable to use as an argument to writelines(). This way there are no pure Python loops, or even generator expressions/comprehensions. ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Files Merging
On Thu, Oct 11, 2012 at 8:30 AM, eryksun eryk...@gmail.com wrote: On Thu, Oct 11, 2012 at 7:13 AM, Sunil Tech sunil.tech...@gmail.com wrote: text1 contains This is from Text1 --- 1st line text2 contains This is from Text2 --- 1st line i want result in text3 like This is from Text1 --- 1st line This is from Text2 --- 1st line zip gets you tuples. map can operate on those tuples I just tried this: x = [1,2,3] y = [4,5,6] def print_2(t): ... print t[0], t[1] ... z = zip(x,y) z [(1, 4), (2, 5), (3, 6)] r = map(print_2, z) 1 4 2 5 3 6 You need to write a function that writes the tuple to a file. It will look something like my print_2() function -- Joel Goldstick ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Files Merging
Thanks all for your immediate responses :) On Thu, Oct 11, 2012 at 6:20 PM, Joel Goldstick joel.goldst...@gmail.comwrote: On Thu, Oct 11, 2012 at 8:30 AM, eryksun eryk...@gmail.com wrote: On Thu, Oct 11, 2012 at 7:13 AM, Sunil Tech sunil.tech...@gmail.com wrote: text1 contains This is from Text1 --- 1st line text2 contains This is from Text2 --- 1st line i want result in text3 like This is from Text1 --- 1st line This is from Text2 --- 1st line zip gets you tuples. map can operate on those tuples I just tried this: x = [1,2,3] y = [4,5,6] def print_2(t): ... print t[0], t[1] ... z = zip(x,y) z [(1, 4), (2, 5), (3, 6)] r = map(print_2, z) 1 4 2 5 3 6 You need to write a function that writes the tuple to a file. It will look something like my print_2() function -- Joel Goldstick ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor