On 2020-03-19 18:22:34 +1300, DL Neil via Python-list wrote:
> On 19/03/20 3:28 PM, Santiago Basulto wrote:
> > myself missing A LOT features from NumPy, like fancy indexing or
> > boolean arrays.
> > So, has it ever been considered to bake into Python's builtin list and
> > dictionary types functionality inspired by NumPy? I think multi indexing
> > alone would be huge addition. A few examples:
> > For lists and tuples:
> >      >>> l = ['a', 'b', 'c']
> >      >>> l[[0, -1]]
> >      ['a', 'c']
> > For dictionaries it'd even be more useful:
> >      d = {
> >          'first_name': 'Frances',
> >          'last_name': 'Allen',
> >          'email': 'fal...@ibm.com'
> >      }
> >      fname, lname = d[['first_name', 'last_name']]
> 
> 
> I fear that I'm missing your point.
> 
> How is
>       l[[0, -1]] or fname, lname = d[['first_name', 'last_name']]
> any better than
>       l[ 0 ], l[ -1 ] or
>       fname = d[ 'first_name' ]
>       lname = d[ 'last_name' ]

It's more compact, especially, if "d" isn't a one-character variable,
but an expression:

    fname, lname = db[people].employee.object.get(pk=1234)[['first_name', 
'last_name']]

vs.

    fname = db[people].employee.object.get(pk=1234)['first_name']
    lname = db[people].employee.object.get(pk=1234)['last_name']

Plus the latter probably performs two database lookups, so you would
want to write:

    person = db[people].employee.object.get(pk=1234)
    fname = person['first_name']
    lname = person['last_name']

(This is one of the Perl features I missed when I started using Python)

        hp

-- 
   _  | Peter J. Holzer    | Story must make more sense than reality.
|_|_) |                    |
| |   | h...@hjp.at         |    -- Charles Stross, "Creative writing
__/   | http://www.hjp.at/ |       challenge!"

Attachment: signature.asc
Description: PGP signature

-- 
https://mail.python.org/mailman/listinfo/python-list

Reply via email to