2017-07-17 57 views
0
automobiles = { 
    'germany': ['bmw', 'audi', 'mercedes benz'], 
    'japan': ['honda', 'toyota', 'subaru'], 
    'united states': ['ford', 'gm', 'buick'], 
    'italy': ['alfa romeo', 'ferrari', 'maserati'], 
    'great britain': ['jaguar', 'mini', 'aston martin'], 
} 

如何访问不同密钥中的各个值? (例如:谁做我访问了“奥迪”在美国的德国键或“别克”键Python 3:在密钥字典中查找值

回答

1

所以,你有一本字典是谁的键是字符串,谁的价值观是列出

要访问? 'audi'你可以这样做:

print(automobiles['germany'][1]) 
# which prints audi 
0

语法:命令(字典[ '关键'] [指数])

你的情况,这将转化:

print(automobiles['germany'][1])

'奥迪'

print(automobiles['united states'][2])

'别克'

存储在你的字典中的值存储为列表。您可以像列表[n]那样访问列表中的元素,其中n是您希望访问的值的索引。 python的索引从0开始。