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

Avatar de zrhans

Posted by

Deixe um comentário

Faça o login usando um destes métodos para comentar:

Logo do WordPress.com

Você está comentando utilizando sua conta WordPress.com. Sair /  Alterar )

Foto do Facebook

Você está comentando utilizando sua conta Facebook. Sair /  Alterar )

Conectando a %s

Este site utiliza o Akismet para reduzir spam. Saiba como seus dados em comentários são processados.

Site criado com WordPress.com.

%d blogueiros gostam disto: