Hi Pythonistas,
yet again today I ended up writing:
d = [[0] * 5 for _ in range(10)]
And wondered, why don't we have a way to repeat other than looping over
range() and using a dummy variable? This seems like a rather common thing
to do, and while the benefit doesn't seem much, something like th
On 30 March 2017 at 19:18, Markus Meskanen wrote:
> Hi Pythonistas,
>
> yet again today I ended up writing:
>
> d = [[0] * 5 for _ in range(10)]
>
> And wondered, why don't we have a way to repeat other than looping over
> range() and using a dummy variable?
Because it's relatively rare to not us
On Mar 30, 2017 12:53, "Nick Coghlan" wrote:
Because it's relatively rare to not use the loop variable for anything
(even if it's just a debug message), and in the cases where you
genuinely don't use it, a standard idiom can be applied (using a
single or double underscore as a dummy variable), ra
On Thu, Mar 30, 2017 at 12:53 PM, Nick Coghlan wrote:
>
> Because it's relatively rare to not use the loop variable for anything
> (even if it's just a debug message), and in the cases where you
> genuinely don't use it, a standard idiom can be applied (using a
> single or double underscore as a d
If there were to be special syntax for this case, I'd just allow an
empty pattern, such as:
d = [[0] * 5 for in 10]
This is exactly the same as your 'repeat_for' except that it is spelt
'for in', which means there are no new keywords. It would also be
allowed in for-loops in the same way as y
Sorry, that obviously should have been:
d = [[0] * 5 for in range(10)]
So it's not quite exactly the same as your example.
On 30 March 2017 at 11:24, Allan Clark wrote:
> If there were to be special syntax for this case, I'd just allow an
> empty pattern, such as:
>
> d = [[0] * 5 for in
Your example is really repeating two things:
d = [ [0 for _ in range(5)] for _ in range(10) ]
But since list() uses * for repetition, you could write it more concisely
as:
d = [[0] * 5] * 10]
I'm not picking on your specific example. I am only pointing out that
Python gives you the tools you ne
That's not the same, in the OP example you can assert d[0] is not d[1],
while in your code that assertion fails (the list comprehension evaluates
the expression each time creating a new list, your code makes 10 references
to a single 5 element list)
On 30 March 2017 at 14:51, Mark E. Haase wrote
Le 30/03/17 à 15:51, Mark E. Haase a écrit :
I'm not picking on your specific example. I am only pointing out that
Python gives you the tools you need to build nice APIs. If repetition
is an important part of something you're working on, then consider
using itertools.repeat, writing your own d
On 3/30/17, Nick Coghlan wrote:
> On 30 March 2017 at 19:18, Markus Meskanen
> wrote:
>> Hi Pythonistas,
>>
>> yet again today I ended up writing:
>>
>> d = [[0] * 5 for _ in range(10)]
d = [[0]*5]*10 # what about this?
>> And wondered, why don't we have a way to repeat other than looping
On 31 March 2017 at 00:23, Pavol Lisy wrote:
> Just for curiosity - if PEP-501 will be accepted then how many times
> could be fnc called in next code?
>
> eval(i'{fnc()}, ' *3)
Once (the same as f-strings), but then it would throw TypeError, as
unlike strings and other sequences, Interpolati
On 2017-03-30 05:18, Markus Meskanen wrote:
> Hi Pythonistas,
>
> yet again today I ended up writing:
>
> d = [[0] * 5 for _ in range(10)]
>
>
> Thoughts?
It looks like you are initializing matrices. Can you make a helper
function?
d = matrix(shape=(5, 10), default=0)
or maybe use NumPy?
On 03/30/2017 04:23 PM, Pavol Lisy wrote:
On 3/30/17, Nick Coghlan wrote:
On 30 March 2017 at 19:18, Markus Meskanen
wrote:
d = [[0] * 5 for _ in range(10)]
d = [[0]*5]*10 # what about this?
These are not quite the same when the repeated object is mutable. Compare:
>>> matrix1 = [
Just an example among many :)
On Mar 30, 2017 6:07 PM, "Kyle Lahnakoski" wrote:
>
>
> On 2017-03-30 05:18, Markus Meskanen wrote:
> > Hi Pythonistas,
> >
> > yet again today I ended up writing:
> >
> > d = [[0] * 5 for _ in range(10)]
> >
> >
> > Thoughts?
>
> It looks like you are initializing
On 30 March 2017 at 10:51, Mark E. Haase wrote:
> Your example is really repeating two things:
>
> d = [ [0 for _ in range(5)] for _ in range(10) ]
>
> But since list() uses * for repetition, you could write it more concisely
> as:
>
> d = [[0] * 5] * 10]
>
> I'm not picking on your specific examp
On Mar 30, 2017 19:04, "Joao S. O. Bueno" wrote:
On 30 March 2017 at 10:51, Mark E. Haase wrote:
> Your example is really repeating two things:
>
> d = [ [0 for _ in range(5)] for _ in range(10) ]
>
> But since list() uses * for repetition, you could write it more concisely
> as:
>
> d = [[0] *
On 30 March 2017 at 13:10, Markus Meskanen wrote:
>
>
> On Mar 30, 2017 19:04, "Joao S. O. Bueno" wrote:
>
> On 30 March 2017 at 10:51, Mark E. Haase wrote:
>> Your example is really repeating two things:
>>
>> d = [ [0 for _ in range(5)] for _ in range(10) ]
>>
>> But since list() uses * for re
And like I said before, for loop is just another way of doing while loop,
yet nobody's complaining. There's nothing wrong with having two different
ways of doing the same thing, as long as one of them is never the better
way. If we add `repeat`, there's never a reason to use `for _ in range`
anymor
Le 30/03/17 à 19:06, Markus Meskanen a écrit :
And like I said before, for loop is just another way of doing while
loop, yet nobody's complaining. There's nothing wrong with having two
different ways of doing the same thing, as long as one of them is
never the better way. If we add `repeat`, t
On Thu, Mar 30, 2017 at 8:23 PM, Brice PARENT wrote:
>
> Le 30/03/17 à 19:06, Markus Meskanen a écrit :
>
>> And like I said before, for loop is just another way of doing while loop,
>> yet nobody's complaining. There's nothing wrong with having two different
>> ways of doing the same thing, as l
On 30/03/17 18:06, Markus Meskanen wrote:
And like I said before, for loop is just another way of doing while loop,
yet nobody's complaining. There's nothing wrong with having two different
ways of doing the same thing, as long as one of them is never the better
way. If we add `repeat`, there's n
On 3/30/17, Nick Coghlan wrote:
> On 31 March 2017 at 00:23, Pavol Lisy wrote:
>> Just for curiosity - if PEP-501 will be accepted then how many times
>> could be fnc called in next code?
>>
>> eval(i'{fnc()}, ' *3)
>
> Once (the same as f-strings), but then it would throw TypeError, as
> unl
A for loop in python saves an enormous amount of boilerplate code though (I
would post an example, but I'd likely mess up a while loop over an iterator
from memory if I posted it here). The `for x in y` construct saves multiple
lines an an enormous amount of boilerplate and mental strain in the
maj
On 3/30/17, Joshua Morton wrote:
>def repeat_for(func, iters):
> return func() for _ in range(iters)
>
> does what you want without any required syntax changes.
I've got a "SyntaxError: invalid syntax"
PL.
___
Python-ideas mailing list
Pyt
> a huge advantage of REs is that they are common to many
> languages. You can take a regex from grep to Perl to your editor to
> Python. They're not absolutely identical, of course, but the basics
> are all the same. Creating a new search language means everyone has to
> learn anew.
> ChrisA
1)
On 31 March 2017 at 04:08, Pavol Lisy wrote:
> On 3/30/17, Nick Coghlan wrote:
>> On 31 March 2017 at 00:23, Pavol Lisy wrote:
>>> Just for curiosity - if PEP-501 will be accepted then how many times
>>> could be fnc called in next code?
>>>
>>> eval(i'{fnc()}, ' *3)
>>
>> Once (the same as
On Thu, Mar 30, 2017 at 04:23:05PM +0200, Pavol Lisy wrote:
> On 3/30/17, Nick Coghlan wrote:
> > On 30 March 2017 at 19:18, Markus Meskanen
> > wrote:
> >> Hi Pythonistas,
> >>
> >> yet again today I ended up writing:
> >>
> >> d = [[0] * 5 for _ in range(10)]
>
> d = [[0]*5]*10 # what abo
On 31 March 2017 at 03:06, Markus Meskanen wrote:
> And like I said before, for loop is just another way of doing while loop,
> yet nobody's complaining. There's nothing wrong with having two different
> ways of doing the same thing, as long as one of them is never the better
> way. If we add `rep
On 31 March 2017 at 15:12, Nick Coghlan wrote:
> So *if* we were to add anything to the language here, it would be to
> add `itertools.repeat_call` as a new iteration primitive, since it
> isn't entirely straightforward to construct that operation out of the
> existing primitives, with itertools.s
On Thu, Mar 30, 2017 at 12:59:57PM +0300, Markus Meskanen wrote:
> And instead of learning a special syntax, which is simple and easy to
> understand when they google "repeat many times python", they now end up
> learning a special semantic by naming the variable with an underscore.
Let me prefac
On Thursday 30 March 2017 02:48 PM, Markus Meskanen wrote:
Hi Pythonistas,
yet again today I ended up writing:
d = [[0] * 5 for _ in range(10)]
And wondered, why don't we have a way to repeat other than looping over
range() and using a dummy variable? This seems like a rather common
thing to d
31 matches
Mail list logo