Learn More Pythonic Way to Launch the X Times Process

What is more pythonic?

So far the loop:

count = 0 while count < 50: print "Some thing" count = count + 1 

For the loop:

 for i in range(50): print "Some thing" 

Edit: do not duplicate, because it has answers to determine what is clearer, and how to start a range without "i" - although this turned out to be the most elegant

+54
python loops
Nov 24 '10 at 8:10
source share
5 answers

Personally:

 for _ in range(50): print "Some thing" 

if you do not need i . If you use Python <3, and you want to repeat the loop many times, use xrange , since you do not need to generate the entire list in advance.

+69
Nov 24 '10 at 8:13
source share

If you encounter side effects that happen in a loop, I personally come up with the range() approach.

If you care about the results of any functions that you call in the loop, I would go on understanding the list or map . Something like that:

 def f(n): return n * n results = [f(i) for i in range(50)] # or using map: results = map(f, range(50)) 
+1
Nov 24 '10 at 8:13
source share

The for loop is definitely more pythonic, as it uses a higher level of Python, built-in functionality, to convey what you do both more clearly and concisely. The overhead for the vs xrange range and the assignment of the unused variable i due to the lack of an operator of type Verilog repeat . The main reason to stick with a range solution is because other methods are more complex. For example:

 from itertools import repeat for unused in repeat(None, 10): del unused # redundant and inefficient, the name is clear enough print "This is run 10 times" 

Using repetition instead of a range is less clear here because it is not so well known and more complex because you need to import it. Basic style guides if you need a reference, PEP 20 - Zen of Python and PEP 8 - Style Guide for Python Code .

We also note that the range version is an explicit example used in both the language reference and the tutorial , although a value is used in this case. This means that the form should be more familiar than extending the cycle of cycle C.

0
Dec 15 '14 at 11:36
source share

What about?

 while BoolIter(N, default=True, falseIndex=N-1): print 'some thing' 

or in an uglier way:

 for _ in BoolIter(N): print 'doing somthing' 

or if you want to intercept the last time:

 for lastIteration in BoolIter(N, default=False, trueIndex=N-1): if not lastIteration: print 'still going' else: print 'last time' 

Where:

 class BoolIter(object): def __init__(self, n, default=False, falseIndex=None, trueIndex=None, falseIndexes=[], trueIndexes=[], emitObject=False): self.n = n self.i = None self._default = default self._falseIndexes=set(falseIndexes) self._trueIndexes=set(trueIndexes) if falseIndex is not None: self._falseIndexes.add(falseIndex) if trueIndex is not None: self._trueIndexes.add(trueIndex) self._emitObject = emitObject def __iter__(self): return self def next(self): if self.i is None: self.i = 0 else: self.i += 1 if self.i == self.n: raise StopIteration if self._emitObject: return self else: return self.__nonzero__() def __nonzero__(self): i = self.i if i in self._trueIndexes: return True if i in self._falseIndexes: return False return self._default def __bool__(self): return self.__nonzero__() 
-3
Jun 09 '15 at 11:47
source share

There is no truly pythonic way of repeating something. However, this is the best way:

map (lambda index: do_something (), xrange (10))

if you need to pass an index, then:

map (lambda index: do_something (index), xrange (10))

Keep in mind that it returns the results as a collection, so if you need to collect results that can help.

-5
Dec 15 '14 at 11:01
source share



All Articles