Re: [Tutor] Process list elements as consecutive pairs
Rüdiger Wolf wrote: > I am trying to Process list elements as consecutive pairs into > consecutive pairs. > Any pythonic suggestions? > > listin = [1,2,3,4,5,6,7,8,9,10] > I want to process as consecutive pairs > 1,2 > 3,4 > 5,6 > 7,8 > 9,10 >>> listin = [1,2,3,4,5,6,7,8,9,10] >>> it = iter(listin) >>> zip(it, it) [(1, 2), (3, 4), (5, 6), (7, 8), (9, 10)] If listin as an odd length the last item will be lost. Peter ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Process list elements as consecutive pairs
On Fri, Mar 5, 2010 at 10:48 PM, Hugo Arts wrote: > > Except the OP requested pairs (1, 2), (3, 4), i.e. with no duplicate > elements. Here is a generator that does what you need: > > def pairs(seq): > it = iter(seq) > try: > while True: > yield it.next(), it.next() > except StopIteration: > return > > Hugo > Just Noticed this tiny caveat: If the length of the sequence is uneven, the last element will not be yielded by this generator. Whether that's a problem, and if it is, how to handle it, depends on the application you're working with. I did make a tiny modification to yield the last element in a pair with None as the second value. The benefit is that it doesn't skip any values, but you'll need to handle the possible None value in your code. Of course, if you want a generator that yields consecutive pairs, passing it a sequence of uneven length is sort of problematic to begin with. So for most applications the simple version should be fine. def pairs(seq): it = iter(seq) try: while True: a = it.next() b = it.next() yield a, b except StopIteration: if len(seq) % 2: yield a, None It's not as pretty as the simple version, unfortunately. in my experience, corner cases are rarely handleable by elegant code :( Compare output of the first and second version: >>> # first (elegant) version of pairs >>> list(pairs(range(5))) [(0, 1), (2, 3)] >>> # second version >>> list(pairs_2(range(5))) [(0, 1), (2, 3), (4, None)] ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Process list elements as consecutive pairs
2010/3/5 Emad Nawfal (عمـ نوفل ـاد) : > > > Here is a general solution that I also took from the Tutor list some time > ago. It allows you to have consequences of (any) length. > def makeVectors(length, listname): > ... """takes the length of the vector and a listname returns vectors""" > ... vectors = (listname[i:i+length] for i in > range(len(listname)-length+1)) > ... return vectors > ... myList = [1,2,3,4,5,6] > bigrams = makeVectors(2, myList) bigrams > at 0xb7227e64> for bigram in bigrams: print bigram > ... > [1, 2] > [2, 3] > [3, 4] > [4, 5] > [5, 6] Except the OP requested pairs (1, 2), (3, 4), i.e. with no duplicate elements. Here is a generator that does what you need: def pairs(seq): it = iter(seq) try: while True: yield it.next(), it.next() except StopIteration: return Hugo ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Process list elements as consecutive pairs
On Fri, Mar 5, 2010 at 1:03 PM, Wayne Werner wrote: > On Fri, Mar 5, 2010 at 11:56 AM, Rüdiger Wolf < > rudiger.w...@throughputfocus.com> wrote: > >> Hi >> >> I am trying to Process list elements as consecutive pairs into >> consecutive pairs. >> Any pythonic suggestions? >> >> listin = [1,2,3,4,5,6,7,8,9,10] >> I want to process as consecutive pairs >> 1,2 >> 3,4 >> 5,6 >> 7,8 >> 9,10 >> > > for x in xrange(0, len(listin), 2): > print listin[x], listin[x+1] > > - that's probably how I'd do it anyway. > > HTH, > Wayne > > ___ > Tutor maillist - Tutor@python.org > To unsubscribe or change subscription options: > http://mail.python.org/mailman/listinfo/tutor > > Here is a general solution that I also took from the Tutor list some time ago. It allows you to have consequences of (any) length. >>> def makeVectors(length, listname): ... """takes the length of the vector and a listname returns vectors""" ... vectors = (listname[i:i+length] for i in range(len(listname)-length+1)) ... return vectors ... >>> myList = [1,2,3,4,5,6] >>> bigrams = makeVectors(2, myList) >>> bigrams at 0xb7227e64> >>> for bigram in bigrams: print bigram ... [1, 2] [2, 3] [3, 4] [4, 5] [5, 6] >>> -- لا أعرف مظلوما تواطأ الناس علي هضمه ولا زهدوا في إنصافه كالحقيقة.محمد الغزالي "No victim has ever been more repressed and alienated than the truth" Emad Soliman Nawfal Indiana University, Bloomington ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Process list elements as consecutive pairs
On Fri, Mar 5, 2010 at 11:56 AM, Rüdiger Wolf < rudiger.w...@throughputfocus.com> wrote: > Hi > > I am trying to Process list elements as consecutive pairs into > consecutive pairs. > Any pythonic suggestions? > > listin = [1,2,3,4,5,6,7,8,9,10] > I want to process as consecutive pairs > 1,2 > 3,4 > 5,6 > 7,8 > 9,10 > for x in xrange(0, len(listin), 2): print listin[x], listin[x+1] - that's probably how I'd do it anyway. HTH, Wayne ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Process list elements as consecutive pairs
On Fri, Mar 5, 2010 at 12:56 PM, Rüdiger Wolf < rudiger.w...@throughputfocus.com> wrote: > I am trying to Process list elements as consecutive pairs into > consecutive pairs. > Any pythonic suggestions? > > listin = [1,2,3,4,5,6,7,8,9,10] > I want to process as consecutive pairs > 1,2 > 3,4 > 5,6 > 7,8 > 9,10 > Not sure it's pythonic but zip(listin[0::2],listin[1::2]) ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor