Difficulty finding a Python 3.x implementation of the familiar C for-loop -


i'm inexperienced in python , started python 3.4.

i read on python 3.x documentation on loop idioms, , haven't found way of constructing familiar c-family for-loop, i.e.

   (i = 0; < n; i++) {        a[i] = value;    } 

writing for-loop in python seems impossible design. know reason why python iteration on sequence follows pattern like

for x in iterable: # e.g. range, itertools.count, generator functions     pass; 

is more efficient, convenient, or reduces index-out-of-bounds exception?

for lower <= var < upper: 

that the proposed syntax c-style loop. "was proposed syntax", because pep 284 rejected, because:

specifically, guido did not buy premise range() format needed fixing, "the whole point (15 years ago) of range() *avoid* needing syntax specify loop on numbers. think it's worked out , there's nothing needs fixed (except range() needs become iterator, in python 3.0)."

so no for lower <= var < upper: us.

now, how c-style loop? well, can use range([start,]end[,step]).

for in range(0,len(blah),3):     blah[i] += merp #alters every third element of blah                     #step defaults 1 if left off 

you can enumerate if need both index , value:

for i,j in enumerate(blah):     merp[j].append(i) 

if wanted @ 2 (or more!) iterators can zip them (also: itertools.izip , itertools.izip_longest)

for i,j in zip(foo,bar):     if == j: print("scooby-doo!")  

and finally, there's while loop

i = 0 while < upper:     a[i] = b     i++ 

addendum: there's pep 276, suggested making ints iterable, rejected. still have been half-open


Comments

Popular posts from this blog

javascript - Using jquery append to add option values into a select element not working -

Android soft keyboard reverts to default keyboard on orientation change -

Rendering JButton to get the JCheckBox behavior in a JTable by using images does not update my table -