📜  在Python中创建具有列表理解的字典

📅  最后修改于: 2022-05-13 01:55:19.972000             🧑  作者: Mango

在Python中创建具有列表理解的字典

在本文中,我们将讨论如何在Python中创建具有列表理解的字典。

方法一:使用 dict() 方法

使用 dict() 方法,我们可以将列表理解转换为字典。在这里,我们将像元组值列表一样传递 list_comprehension,这样第一个值充当字典中的键,第二个值充当字典中的值。

示例 1 :创建学生列表理解并将其转换为字典的Python程序。

Python3
# create a list comprehension with student age
data = [('sravan', 23), ('ojaswi', 15),
        ('rohith', 8), ('gnanesh', 4), ('bobby', 20)]
 
# using dict method
dict(data)


Python3
# create a list comprehension with student age
data = [('sravan', 23), ('ojaswi', 15),
        ('rohith', 8), ('gnanesh', 4), ('bobby', 20)]
 
# using dict method inside for loop
dict([(key, value) for key, value in data])


Python3
# create a list with student name
name = ['sravan', 'ojaswi', 'rohith', 'gnanesh', 'bobby']
 
# create a list with student age
age = [23, 21, 32, 11, 23]
 
# using dict method with zip()
dict(zip(name, age))


Python3
# create a list comprehension with student age
data = [('sravan', 23), ('ojaswi', 15),
        ('rohith', 8), ('gnanesh', 4), ('bobby', 20)]
 
 
# display using iterable method
{key: value for (key, value) in data}


Python3
# create a list comprehension with student age
data = [('sravan', 23), ('ojaswi', 15),
        ('rohith', 8), ('gnanesh', 4), ('bobby', 20)]
 
 
# create a dictionary with list
# comprehension if value is equal to 20
print({key: value for (key, value) in data if value == 20})
 
# create a dictionary with list
# comprehension if value is greater than  to 10
print({key: value for (key, value) in data if value > 10})
 
# create a dictionary with list
# comprehension if key is sravan
print({key: value for (key, value) in data if key == 'sravan'})


输出:

{'bobby': 20, 'gnanesh': 4, 'ojaswi': 15, 'rohith': 8, 'sravan': 23}

我们还可以在 dict() 方法中使用以下 for 循环。

示例 2

Python3

# create a list comprehension with student age
data = [('sravan', 23), ('ojaswi', 15),
        ('rohith', 8), ('gnanesh', 4), ('bobby', 20)]
 
# using dict method inside for loop
dict([(key, value) for key, value in data])

输出:

{'bobby': 20, 'gnanesh': 4, 'ojaswi': 15, 'rohith': 8, 'sravan': 23}

方法 2:使用 zip() 和 dict() 方法

在此方法中,我们将创建两个列表,第一个列表中的值将是键,第二个列表值将是字典中的值。

示例 1

Python3

# create a list with student name
name = ['sravan', 'ojaswi', 'rohith', 'gnanesh', 'bobby']
 
# create a list with student age
age = [23, 21, 32, 11, 23]
 
# using dict method with zip()
dict(zip(name, age))

输出

{'bobby': 23, 'gnanesh': 11, 'ojaswi': 21, 'rohith': 32, 'sravan': 23}

方法3:使用Iterable

在这里,我们可以使用可迭代的 for 循环遍历给定的日期。

示例

Python3

# create a list comprehension with student age
data = [('sravan', 23), ('ojaswi', 15),
        ('rohith', 8), ('gnanesh', 4), ('bobby', 20)]
 
 
# display using iterable method
{key: value for (key, value) in data}

输出:

{'bobby': 20, 'gnanesh': 4, 'ojaswi': 15, 'rohith': 8, 'sravan': 23}

方法四:添加过滤器

我们可以将过滤器添加到列表推导的可迭代中,以根据条件仅为特定数据创建字典。过滤意味着根据条件将值添加到字典中。

示例

Python3

# create a list comprehension with student age
data = [('sravan', 23), ('ojaswi', 15),
        ('rohith', 8), ('gnanesh', 4), ('bobby', 20)]
 
 
# create a dictionary with list
# comprehension if value is equal to 20
print({key: value for (key, value) in data if value == 20})
 
# create a dictionary with list
# comprehension if value is greater than  to 10
print({key: value for (key, value) in data if value > 10})
 
# create a dictionary with list
# comprehension if key is sravan
print({key: value for (key, value) in data if key == 'sravan'})

输出:

{'bobby': 20}
{'sravan': 23, 'ojaswi': 15, 'bobby': 20}
{'sravan': 23}