You are not logged in.
Pages: 1
Often, I need to make a couple trips through a loop, but do not need to reference to the number of times I have been through the loop. Example:
for i in range(2):
location = self.Move(location,options[direction])
Here, I have an array of directions I can move, and I want to move two spaces an a given direction. To do this, I want to make two moves. I have no need of the index i. Is there a more Pythonic way of doing this where I do not need to declare a variable I never use?
Edit: location is a 2nd order Tuple, options is a Tuple of vectors (2nd order Tuples that are added element by element to location), direction is an integer
Last edited by ewaller (2014-02-15 01:07:39)
Nothing is too wonderful to be true, if it be consistent with the laws of nature -- Michael Faraday
Sometimes it is the people no one can imagine anything of who do the things no one can imagine. -- Alan Turing
---
How to Ask Questions the Smart Way
Offline
Unused variables often get only an underscore as the name. This convention may be used by some python interpreters to improve the execution.
Last edited by progandy (2014-02-15 01:15:13)
| alias CUTF='LANG=en_XX.UTF-8@POSIX ' |
Offline
Python 3.3.3 (default, Nov 26 2013, 13:33:18)
[GCC 4.8.2] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> for _ in range (2): print ("foo")
...
foo
foo
>>> for _ in range (2): print ("foo"): print (_)
File "<stdin>", line 1
for _ in range (2): print ("foo"): print (_)
^
SyntaxError: invalid syntax
>>>
Interesting. Thank you.
edit:
>>> for _ in range (2): print ("foo") print (_)
File "<stdin>", line 1
for _ in range (2): print ("foo") print (_)
^
SyntaxError: invalid syntax
>>> for _ in range (2): print (_)
...
0
1
>>>
Oh, not so interesting, but still interesting
Last edited by ewaller (2014-02-15 01:13:02)
Nothing is too wonderful to be true, if it be consistent with the laws of nature -- Michael Faraday
Sometimes it is the people no one can imagine anything of who do the things no one can imagine. -- Alan Turing
---
How to Ask Questions the Smart Way
Offline
If you want to improve speed, use itertools.repeat(None, times) instead of range(times), it is a bit faster because it doesn't have to generate new python objects but returns the same "None" every time.
Edit: In python2, you have also xrange (return a generator) and range (returns a list). In python3 range was removed and xrange renamed to range.
Last edited by progandy (2014-02-15 01:17:42)
| alias CUTF='LANG=en_XX.UTF-8@POSIX ' |
Offline
That is exactly what I was looking for Thanks!
Nothing is too wonderful to be true, if it be consistent with the laws of nature -- Michael Faraday
Sometimes it is the people no one can imagine anything of who do the things no one can imagine. -- Alan Turing
---
How to Ask Questions the Smart Way
Offline
Pages: 1