Python - 在字典中访问列表中的项目
给定一个值作为列表的字典,任务是编写一个Python程序,该程序可以访问该字典中的列表值项。
方法一:手动访问列表中的项目
这是一种直接的方法,其中必须从中提取值的键与特定值的索引一起传递。
句法:
dictionary_name[key][index]
示例:直接索引
Python3
# Creating dictionary which contains lists
country = {
"India": ["Delhi", "Maharastra", "Haryana",
"Uttar Pradesh", "Himachal Pradesh"],
"Japan": ["Hokkaido", "Chubu", "Tohoku", "Shikoku"],
"United States": ["New York", "Texas", "Indiana",
"New Jersey", "Hawaii", "Alaska"]
}
print(country["India"])
print(country["India"][0])
print(country["India"][1])
print(country["United States"][3])
print(country['Japan'][2])
Python3
# Creating dictionary which contains lists
country = {
"India": ["Delhi", "Maharastra", "Haryana",
"Uttar Pradesh", "Himachal Pradesh"],
"Japan": ["Hokkaido", "Chubu", "Tohoku", "Shikoku"],
"United States": ["New York", "Texas", "Indiana",
"New Jersey", "Hawaii", "Alaska"]
}
for key, val in country.items():
for i in val:
print("{} : {}".format(key, i))
print("--------------------")
Python3
# Creating dictionary which contains lists
country = {
"India": ["Delhi", "Maharastra", "Haryana",
"Uttar Pradesh", "Himachal Pradesh"],
"Japan": ["Hokkaido", "Chubu", "Tohoku", "Shikoku"],
"United States": ["New York", "Texas", "Indiana",
"New Jersey", "Hawaii", "Alaska"]
}
for i in country['Japan']:
print(i)
for i in country['India']:
print(i)
for i in country['United States']:
print(i)
Python3
# Creating dictionary which contains lists
country = {
"India": ["Delhi", "Maharastra", "Haryana",
"Uttar Pradesh", "Himachal Pradesh"],
"Japan": ["Hokkaido", "Chubu", "Tohoku", "Shikoku"],
"United States": ["New York", "Texas", "Indiana",
"New Jersey", "Hawaii", "Alaska"]
}
# extract the first 3 cities of India
print(country["India"][:3])
# extract last 2 cities from Japan
print(country["Japan"][-2:])
# extract all cities except last 3 cities from india
print(country["India"][:-3])
# extract 2th to 5th cities from us
print(country["United States"][1:5])
输出 :
[‘Delhi’, ‘Maharastra’, ‘Haryana’, ‘Uttar Pradesh’, ‘Himachal Pradesh’]
Delhi
Maharastra
New Jersey
Tohoku
方法二:使用循环
完成给定任务的最简单方法是迭代字典。
示例:使用循环
蟒蛇3
# Creating dictionary which contains lists
country = {
"India": ["Delhi", "Maharastra", "Haryana",
"Uttar Pradesh", "Himachal Pradesh"],
"Japan": ["Hokkaido", "Chubu", "Tohoku", "Shikoku"],
"United States": ["New York", "Texas", "Indiana",
"New Jersey", "Hawaii", "Alaska"]
}
for key, val in country.items():
for i in val:
print("{} : {}".format(key, i))
print("--------------------")
输出 :
India : Delhi
India : Maharastra
India : Haryana
India : Uttar Pradesh
India : Himachal Pradesh
——————–
Japan : Hokkaido
Japan : Chubu
Japan : Tohoku
Japan : Shikoku
——————–
United States : New York
United States : Texas
United States : Indiana
United States : New Jersey
United States : Hawaii
United States : Alaska
——————–
方法3 :访问密钥的特定列表
这或多或少是前两种方法的组合,其中使用键迭代值列表。
示例:访问特定的密钥列表
蟒蛇3
# Creating dictionary which contains lists
country = {
"India": ["Delhi", "Maharastra", "Haryana",
"Uttar Pradesh", "Himachal Pradesh"],
"Japan": ["Hokkaido", "Chubu", "Tohoku", "Shikoku"],
"United States": ["New York", "Texas", "Indiana",
"New Jersey", "Hawaii", "Alaska"]
}
for i in country['Japan']:
print(i)
for i in country['India']:
print(i)
for i in country['United States']:
print(i)
输出:
Hokkaido
Chubu
Tohoku
Shikoku
Delhi
Maharastra
Haryana
Uttar Pradesh
Himachal Pradesh
New York
Texas
Indiana
New Jersey
Hawaii
Alaska
方法4 :使用列表切片
这是第一种方法的修改版本,这里我们传递切片范围而不是值列表的索引。
句法:
dictionary_name[key][start_index : end_index]
示例:使用列表切片
蟒蛇3
# Creating dictionary which contains lists
country = {
"India": ["Delhi", "Maharastra", "Haryana",
"Uttar Pradesh", "Himachal Pradesh"],
"Japan": ["Hokkaido", "Chubu", "Tohoku", "Shikoku"],
"United States": ["New York", "Texas", "Indiana",
"New Jersey", "Hawaii", "Alaska"]
}
# extract the first 3 cities of India
print(country["India"][:3])
# extract last 2 cities from Japan
print(country["Japan"][-2:])
# extract all cities except last 3 cities from india
print(country["India"][:-3])
# extract 2th to 5th cities from us
print(country["United States"][1:5])
输出 :
[‘Delhi’, ‘Maharastra’, ‘Haryana’]
[‘Tohoku’, ‘Shikoku’]
[‘Delhi’, ‘Maharastra’]
[‘Texas’, ‘Indiana’, ‘New Jersey’, ‘Hawaii’]