Quelle est cette syntaxe ?
Bonjour,
Je suis débutant en Python et j'étudie donc des morceaux de codes pour progresser, je suis tombé sur celui-ci, et je ne comprends pas
comment l'utilisation des instructions for et if est possible de cette manière, à savoir dans une parenthèse, sans indentation et sur une seule ligne.
J'ai effectué pas mal de recherche, je ne trouve pas, pouvez-vous m'en dire plus sur cette façon de programmer ? Est-ce uniquement utilisable
avec des dictionnaires, des listes ?
Ce code est tiré de projet:
https://github.com/RobotTraders/Pyth...hapter_3.ipynb
Merci
Code:
1 2 3 4 5 6 7 8 9
|
# 4. Add to the dictionary the average purchase price for each of the cryptos
for symbol in portfolio:
total_quantity_bought = sum([t["quantity"] for t in transactions if t["symbol"] == symbol and t["type"] == "buy"])
total_spent = sum([t["price"] * t["quantity"] for t in transactions if t["symbol"] == symbol and t["type"] == "buy"])
avg_price = total_spent / total_quantity_bought
portfolio[symbol]["avg_price"] = avg_price
print(portfolio) |