filmov
tv
Python dictionary comprehension 🕮
Показать описание
Python dictionary comprehension tutorial example explained
#python #dictionary #comprehension
# -------------------------------------------------------------------------
# dictionary comprehension = create dictionaries using an expression
# can replace for loops and certain lambda functions
#
# dictionary = {key: expression for (key,value) in iterable}
# dictionary = {key: expression for (key,value) in iterable if conditional}
# dictionary = {key: (if/else) for (key,value) in iterable}
# dictionary = {key: function(value) for (key,value) in iterable}
# -------------------------------------------------------------------------
cities_in_F = {'New York': 32, 'Boston': 75, 'Los Angeles': 100, 'Chicago': 50}
cities_in_C = {key: round((value-32)*(5/9)) for (key,value) in cities_in_F.items()}
print(cities_in_C)
# -------------------------------------------------------------------------
# weather = {'New York': "snowing", 'Boston': "sunny", 'Los Angeles': "sunny", 'Chicago': "cloudy"}
# print(sunny_weather)
# -------------------------------------------------------------------------
Bro Code merch store 👟 :
===========================================================
===========================================================
music credits 🎼 :
===========================================================
Creative Commons — Attribution-ShareAlike 3.0 Unported— CC BY-SA 3.0
===========================================================
#python #dictionary #comprehension
# -------------------------------------------------------------------------
# dictionary comprehension = create dictionaries using an expression
# can replace for loops and certain lambda functions
#
# dictionary = {key: expression for (key,value) in iterable}
# dictionary = {key: expression for (key,value) in iterable if conditional}
# dictionary = {key: (if/else) for (key,value) in iterable}
# dictionary = {key: function(value) for (key,value) in iterable}
# -------------------------------------------------------------------------
cities_in_F = {'New York': 32, 'Boston': 75, 'Los Angeles': 100, 'Chicago': 50}
cities_in_C = {key: round((value-32)*(5/9)) for (key,value) in cities_in_F.items()}
print(cities_in_C)
# -------------------------------------------------------------------------
# weather = {'New York': "snowing", 'Boston': "sunny", 'Los Angeles': "sunny", 'Chicago': "cloudy"}
# print(sunny_weather)
# -------------------------------------------------------------------------
Bro Code merch store 👟 :
===========================================================
===========================================================
music credits 🎼 :
===========================================================
Creative Commons — Attribution-ShareAlike 3.0 Unported— CC BY-SA 3.0
===========================================================
Комментарии