Dicionários em python

Portal Física

Original: Dan Bader – danbader.org

When I learn a new trick for my “Python coding toolbox” I often sense some benefit right away.

It’s like I KNOW this thing is useful for something—

And yet I’m sitting here banging my head against the table trying to find a practical application for it.

How do you take a new function or module you heard about, and turn it into a sprinkling of Pythonic fairy dust that gets you a “ooh, nice!” comment in your next code review?

The other day I got this question from newsletter reader Paul, in response to a piece I wrote about Python’s “enumerate()” function:

To give you some more context, this is what Paul wants to do:

input = ['Duration', 'F0', 'F1', 'F2', 'F3']
output = {'Duration': 0, 'F0': 1, 'F1': 2, 'F2': 3, 'F3': 4}

And this is how he accomplished it:

>>> {f:i for…

Ver o post original 382 mais palavras