Python中的字典
时间:2020-02-23 14:42:16 来源:igfitidea点击:
本文简要介绍了Python中的字典理解。
Python字典是Python语言的一项非常有用的功能。
它用作键与其值之间的映射。
有时,将值分配给特定键可能对程序员而言过于繁琐。
因此,Python提供了词典理解功能,以节省用户的宝贵时间。
基本的Python字典
# Dictionary stores 1 for odd keys, 0 for even keys odd = {key: key % 2 for key in range(1, 10)} # Printing the dictionary print(odd)
输出:
{1: 1, 2: 0, 3: 1, 4: 0, 5: 1, 6: 0, 7: 1, 8: 0, 9: 1}
单行代码涉及一些元素。
让我们逐一探究它们。
变量–
odd
–存储所有信息的字典变量。Key –
key
–字典的键。值–"键%2" –对应于键的值。
迭代器–"用于键" –迭代器的工作是存储每次迭代的相关值。
Iterable –
range(1,10)
– Iterable负责循环的功能并为迭代器赋值。
一些说明性的例子
需要遍历更多示例,以能够在日常编码中使用字典理解。
转换温度
# Temperatures in Celcius celcius = {"Mumbai":36.5, "Delhi":27.6, "Bangalore":32.1, "Dholakpur":40.4} # Temperature in Fahrenheit fahrenheit = {key: value*(9/5) + 32 for key,value in celcius.items()} # Printing the temperatures print(fahrenheit)
输出:
{'Mumbai': 97.7, 'Delhi': 81.68, 'Bangalore': 89.78, 'Dholakpur': 104.72}
在上面的示例中,字典理解会遍历另一个字典的各项,并根据设计的公式分配键和值对。
元素的存在
# A list of integers lis = [2, 5, 6, 12, 9, 7] # A dictionary for look-up if element is present in list present = {key: 1 for key in lis} # Printing the Dictionary print(present)
输出:
{2: 1, 5: 1, 6: 1, 12: 1, 9: 1, 7: 1}
列表到字典的这种转换减少了任何查找查询的时间复杂度。
可以以O(1)复杂度而不是O(N)来检查元素是否存在。
反转字典
# A dictionary containing numbers in integers and strings numbers = {1: "one", 2: "two", 3:"three", 4: "four", 5: "five"} # Inverting a Python dictionary invert_numbers = {value: key for key, value in numbers.items()} # Printing the inverted dictionary print(invert_numbers)
输出:
{'one': 1, 'two': 2, 'three': 3, 'four': 4, 'five': 5}
字典理解的最有用的实现之一是反转Python字典。
上面的代码切换键和值对。
条件字典理解
可以将另一个元素添加到字典理解中-条件语句。
这些语句可用于根据键或者值过滤字典内容。
# Temperatures temp = {"Mumbai":36.5, "Delhi":27.6, "Bangalore":32.1, "Dholakpur":40.4} # Places with high temperature hot = {key: value for key, value in temp.items() if value >= 35} # Places with low temperatures cold = {key: value for key, value in temp.items() if value < 35} # Printing the dictionaries print("Hot places =", hot) print("Cold places =", cold)
输出:
Hot places = {'Mumbai': 36.5, 'Dholakpur': 40.4} Cold places = {'Delhi': 27.6, 'Bangalore': 32.1}
如果将值和键的集合固定在通过字典理解生成的字典中,则将条件语句放置在迭代部分之后。
如果存在与字典值相关的if-else条件,则必须将条件放在迭代段之前。
# Temperatures temp = {"Mumbai":36.5, "Delhi":27.6, "Bangalore":32.1, "Dholakpur":40.4} # Places with their type of temperature hot_or_cold = {key: ('hot' if value >= 35 else 'cold') for key, value in temp.items()} # Printing the dictionary print("Hot places =", hot_or_cold)
输出:
Hot places = {'Mumbai': 'hot', 'Delhi': 'cold', 'Bangalore': 'cold', 'Dholakpur': 'hot'}
多个条件语句
# Temperatures temp = {"Mumbai":36.5, "Delhi":27.6, "Bangalore":32.1, "Dholakpur":40.4} # Hot places starting with M hot_M = {key: value for key, value in temp.items() if value >= 35 if key.startswith('M')} # Printing the dictionary print(hot_M)
输出:
{'Mumbai': 36.5}
在以上代码段中,存在两个过滤器,一个过滤热点,而另一个过滤以'M'开头的位置。
嵌套字典理解
通过嵌套多个字典推导,可以制定复杂的字典。
创建乘法表
# Importing the pretty print library import pprint # Creating multiplication tables tables = {key1:{key2: key1*key2 for key2 in range(1, 10)} for key1 in range(1, 10)} # Printing the multiplication tables pprint.pprint(tables) print() # Fetch multiplication values print("5 x 7 =", tables[5][7]) print("3 x 6 =", tables[3][6]) print("8 x 9 =", tables[9][8])
输出:
{1: {1: 1, 2: 2, 3: 3, 4: 4, 5: 5, 6: 6, 7: 7, 8: 8, 9: 9}, 2: {1: 2, 2: 4, 3: 6, 4: 8, 5: 10, 6: 12, 7: 14, 8: 16, 9: 18}, 3: {1: 3, 2: 6, 3: 9, 4: 12, 5: 15, 6: 18, 7: 21, 8: 24, 9: 27}, 4: {1: 4, 2: 8, 3: 12, 4: 16, 5: 20, 6: 24, 7: 28, 8: 32, 9: 36}, 5: {1: 5, 2: 10, 3: 15, 4: 20, 5: 25, 6: 30, 7: 35, 8: 40, 9: 45}, 6: {1: 6, 2: 12, 3: 18, 4: 24, 5: 30, 6: 36, 7: 42, 8: 48, 9: 54}, 7: {1: 7, 2: 14, 3: 21, 4: 28, 5: 35, 6: 42, 7: 49, 8: 56, 9: 63}, 8: {1: 8, 2: 16, 3: 24, 4: 32, 5: 40, 6: 48, 7: 56, 8: 64, 9: 72}, 9: {1: 9, 2: 18, 3: 27, 4: 36, 5: 45, 6: 54, 7: 63, 8: 72, 9: 81}} 5 x 7 = 35 3 x 6 = 18 8 x 9 = 72
嵌套字典理解缺乏可读性,因此在Python中应避免使用。