遍历Python中的字典列表
在本文中,我们将学习如何遍历字典列表。
使用中的字典列表:
[{‘Python’: ‘Machine Learning’, ‘R’: ‘Machine learning’},
{‘Python’: ‘Web development’, ‘Java Script’: ‘Web Development’, ‘HTML’: ‘Web Development’},
{‘C++’: ‘Game Development’, ‘Python’: ‘Game Development’}, {‘Java’: ‘App Development’, ‘Kotlin’: ‘App Development’}]
方法 1:使用索引
这是一种直接方法,其中仅使用索引提取列表元素。
句法:
list[index]
例子:
Python3
# Create a list of dictionaries
languages = [
{
"Python": "Machine Learning",
"R": "Machine learning",
},
{
"Python": "Web development",
"Java Script": "Web Development",
"HTML": "Web Development"
},
{
"C++": "Game Development",
"Python": "Game Development"
},
{
"Java": "App Development",
"Kotlin": "App Development"
}
]
print(languages[0])
print(languages[1])
print(languages[2])
print(languages[3])
Python3
# Create a list of dictionaries
languages = [
{
"Python": "Machine Learning",
"R": "Machine learning",
},
{
"Python": "Web development",
"Java Script": "Web Development",
"HTML": "Web Development"
},
{
"C++": "Game Development",
"Python": "Game Development"
},
{
"Java": "App Development",
"Kotlin": "App Development"
}
]
for key, val in languages[0].items():
print("{} : {}".format(key, val))
Python3
# Create a list of dictionaries
languages = [
{
"Python": "Machine Learning",
"R": "Machine learning",
},
{
"Python": "Web development",
"Java Script": "Web Development",
"HTML": "Web Development"
},
{
"C++": "Game Development",
"Python": "Game Development"
},
{
"Java": "App Development",
"Kotlin": "App Development"
}
]
# iterate over the list
for i in languages:
# now i is a dict, now we see the keys
# of the dict
for key in i.keys():
# print every key of each dict
print(key)
print("-------------")
Python3
# Create a list of dictionaries
languages = [
{
"Python" : "Machine Learning",
"R" : "Machine learning",
},
{
"Python" : "Web development",
"Java Script" : "Web Development",
"HTML" : "Web Development"
},
{
"C++" : "Game Development",
"Python" : "Game Development"
},
{
"Java" : "App Development",
"Kotlin" : "App Development"
}
]
# here we are printing the keys of the dictonary
# by using list comprehension and each key will be
# printed in a new line due to the presence of " sep = "\n" ".
# It will add a new line character to our output.
print(*[key for i in languages for key in i.keys()], sep = "\n")
输出:
{‘Python’: ‘Machine Learning’, ‘R’: ‘Machine learning’}
{‘Python’: ‘Web development’, ‘Java Script’: ‘Web Development’, ‘HTML’: ‘Web Development’}
{‘C++’: ‘Game Development’, ‘Python’: ‘Game Development’}
{‘Java’: ‘App Development’, ‘Kotlin’: ‘App Development’}
在对特定字典使用索引后,现在我们可以将列表中的每个项目视为字典,
示例:从特定字典中提取值
蟒蛇3
# Create a list of dictionaries
languages = [
{
"Python": "Machine Learning",
"R": "Machine learning",
},
{
"Python": "Web development",
"Java Script": "Web Development",
"HTML": "Web Development"
},
{
"C++": "Game Development",
"Python": "Game Development"
},
{
"Java": "App Development",
"Kotlin": "App Development"
}
]
for key, val in languages[0].items():
print("{} : {}".format(key, val))
输出:
Python : Machine Learning
R : Machine learning
方法二:使用keys()
在迭代到列表之后,可以使用 keys()函数进一步提取字典中的键。
示例:提取键值
蟒蛇3
# Create a list of dictionaries
languages = [
{
"Python": "Machine Learning",
"R": "Machine learning",
},
{
"Python": "Web development",
"Java Script": "Web Development",
"HTML": "Web Development"
},
{
"C++": "Game Development",
"Python": "Game Development"
},
{
"Java": "App Development",
"Kotlin": "App Development"
}
]
# iterate over the list
for i in languages:
# now i is a dict, now we see the keys
# of the dict
for key in i.keys():
# print every key of each dict
print(key)
print("-------------")
输出:
Python
R
————-
Python
Java Script
HTML
————-
C++
Python
————-
Java
Kotlin
————-
方法 3:使用列表理解
使用列表推导简单地迭代列表并打印字典。
示例:使用列表推导式提取键
蟒蛇3
# Create a list of dictionaries
languages = [
{
"Python" : "Machine Learning",
"R" : "Machine learning",
},
{
"Python" : "Web development",
"Java Script" : "Web Development",
"HTML" : "Web Development"
},
{
"C++" : "Game Development",
"Python" : "Game Development"
},
{
"Java" : "App Development",
"Kotlin" : "App Development"
}
]
# here we are printing the keys of the dictonary
# by using list comprehension and each key will be
# printed in a new line due to the presence of " sep = "\n" ".
# It will add a new line character to our output.
print(*[key for i in languages for key in i.keys()], sep = "\n")
输出:
Python
R
Python
Java Script
HTML
C++
Python
Java
Kotlin