[Python-ideas] Proposal: Tuple of str with w'list of words'

Gary Godfrey g.pythonideas at wamp.us
Sat Nov 12 12:01:00 EST 2016


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
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20161112/8b961ad6/attachment.html>


More information about the Python-ideas mailing list