[Python-ideas] Proposal: Tuple of str with w'list of words'
David Mertz
mertz at gnosis.cx
Sat Nov 12 12:33:10 EST 2016
Just spend the extra two characters to do this with existing syntax:
w('field1 field2 field3'). Implementation of the w() function is trivial.
On Nov 12, 2016 9:04 AM, "Gary Godfrey" <g.pythonideas at wamp.us> wrote:
> Hi,
>
> I looked around for a while but didn't see this proposed anywhere. I
> apologize if I missed an existing discussion.
>
> I do a fair amount of work with pandas and data munging. This means that
> I'm often doing things like:
>
> mydf = df[ ['field1', 'field2', 'field3' ] ]
>
> This is a little ugly, so if the list is long enough, I do:
>
> mydf=df[ 'field1 field2 field3'.split() ]
>
> This is a little more readable, but still a bit ugly. What I'm proposing
> here is:
>
> mydf = df[ w'field1 field2 field3' ]
>
> This would be identical in all ways (compile-time) to:
>
> mydf = df[ ('field1', 'field2', 'field3') ]
>
> This should work with all the python quote variations (w''', w""", etc).
> The only internal escapes are \\ indicating a \ and <backslash><space>
> indicating a non-splitting space:
>
> songs = w'My\ Bloody\ Valentine Blue\ Suede\ Shoes'
>
> One question is whether to have w'' be a list or a tuple. I leaned
> slightly towards tuple because it's faster on internal loops:
>
> In [1]: %timeit a=('this','is','a','test')
> 100000000 loops, best of 3: 11.3 ns per loop
>
> In [2]: %timeit a=['this','is','a','test']
> 10000000 loops, best of 3: 74.3 ns per loop
>
> However, I mostly see lists used in the data science community, so it's a
> little less convenient:
>
> other_fields = df.columns[-3:]
> new_columns = w'field1 field2' + other_fields
> # ERROR - can't concatenate list to tuple
> new_columns = list(w'field1 field2') + other_fields
>
> I honestly could go either way with lists or tuples.
>
> Other Languages:
>
> perl has the qw operator:
>
> @a = qw(field1 field2 field3);
>
> ruby has %w
>
> a=%w{field1 field2}
>
> Thanks for reading this far :-)
>
> Regards,
> Gary Godfrey
> Austin, TX
>
>
>
>
> _______________________________________________
> Python-ideas mailing list
> Python-ideas at python.org
> https://mail.python.org/mailman/listinfo/python-ideas
> Code of Conduct: http://python.org/psf/codeofconduct/
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20161112/c21b0973/attachment.html>
More information about the Python-ideas
mailing list