Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/277.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何提取python字典信息 我有以下代码: 我可以通过以下方式提取信息: 但是,我想通过以下方式提取信息: 我想通过以下方式提取信息:_Python_Python 3.x_Dictionary - Fatal编程技术网

如何提取python字典信息 我有以下代码: 我可以通过以下方式提取信息: 但是,我想通过以下方式提取信息: 我想通过以下方式提取信息:

如何提取python字典信息 我有以下代码: 我可以通过以下方式提取信息: 但是,我想通过以下方式提取信息: 我想通过以下方式提取信息:,python,python-3.x,dictionary,Python,Python 3.x,Dictionary,我承认我已经尝试了很多方法,但我无法找到一本带有关键字的字典,而这些关键字是字符串,并存储为哈希列表。因此,您的字典在雪佛兰的地址中存储了Onix。所以我们可以访问它作为 cars['Chevrolet'] {'Onix':'2018车型','Selta':'2017车型'} “2018车型” 如果你不知道某个特定型号的品牌是什么。您可以使用列表来查找它 cars = {'Chevrolet':{'Onix':'Model 2018', 'Selta

我承认我已经尝试了很多方法,但我无法找到一本带有关键字的字典,而这些关键字是字符串,并存储为哈希列表。因此,您的字典在雪佛兰的地址中存储了Onix。所以我们可以访问它作为

cars['Chevrolet']
{'Onix':'2018车型','Selta':'2017车型'}

“2018车型”


如果你不知道某个特定型号的品牌是什么。您可以使用列表来查找它

cars = {'Chevrolet':{'Onix':'Model 2018',
                     'Selta':'Model 2017'},
        'Ford':{'Fusion':'Model 2009',
                      'Ranger':'Model 2015'}}

model = 'Selta'
brand = [car for car in cars for m in cars[car] if m == model][0]

print('Car: ' + model + ' - ' + cars[brand][model])
您只需更改
model
变量,它就会发现可以在哪个品牌下找到该模型


然后,您可以创建一个小的实用程序函数

def getInfo(cars, model):
    brand = [car for car in cars for m in cars[car] if m == model][0]
    return 'Car: ' + model + ' - ' + cars[brand][model]

cars = {'Chevrolet':{'Onix':'Model 2018',
                     'Selta':'Model 2017'},
        'Ford':{'Fusion':'Model 2009',
                      'Ranger':'Model 2015'}}

print(getInfo(cars, 'Ranger'))

你能更清楚地说明最后两个黑体字吗?他们似乎要求做同样的事情,这对我来说确实有帮助。谢谢你的关注和帮助。
>> car(Onix)
'Car: Onix - Model 2018!'
cars['Chevrolet']
cars['Chevrolet']['Onix']
cars = {'Chevrolet':{'Onix':'Model 2018',
                     'Selta':'Model 2017'},
        'Ford':{'Fusion':'Model 2009',
                      'Ranger':'Model 2015'}}

model = 'Selta'
brand = [car for car in cars for m in cars[car] if m == model][0]

print('Car: ' + model + ' - ' + cars[brand][model])
def getInfo(cars, model):
    brand = [car for car in cars for m in cars[car] if m == model][0]
    return 'Car: ' + model + ' - ' + cars[brand][model]

cars = {'Chevrolet':{'Onix':'Model 2018',
                     'Selta':'Model 2017'},
        'Ford':{'Fusion':'Model 2009',
                      'Ranger':'Model 2015'}}

print(getInfo(cars, 'Ranger'))