On Fri, Mar 5, 2010 at 1:03 PM, Wayne Werner <waynejwer...@gmail.com> 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 <generator object <genexpr> 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