Python Nested Dict,如果与键和值兼容,请检查3个不同的版本

时间:2014-08-21 15:37:39

标签: python dictionary

我在论坛中找不到类似的解决方案,抱歉,如果我错过了。 我用view_version构建了一个嵌套的dict:os_name:driver-version,我试图检查view_version,os_name和驱动程序版本(所有3都是从日志文件中解析过的)是否与os_name对应的view_version兼容字典中的driver_version。什么是最好的方法这样做我尝试访问键和值并比较它,但似乎没有工作或给出任何错误。新的python和字典。如果我需要编辑它并让它更清晰,请告诉我。 提前谢谢

version_dict = {}
version_dict = {'view 3.1.3': {'windows XP':'11.6.0.35', 'windows vista': '17.14.1.42',     'windows 7': 'not supported', 'windows 8':'not supported', 'windows 8.1' : 'not supported','windows server 20008 R2':'not supported'},
'view 4.0.2':{'windows xp': '11.6.0.35','windows vista': 'not supported','windows 7': 'not supported','windows 8': 'not supported','windows 8.1': 'not supported', 'windows server 2008 R2': 'not supported'},
'view 4.5.0':{'windows xp': '11.6.0.37','windows vista': '11.6.0.37','windows 7': '7.14.1.49','windows 8': 'not supported','windows 8.1': 'not supported', 'windows server 2008 R2': 'not supported'},
'view 4.6.0':{'windows xp': '11.6.0.37','windows vista': '11.6.0.37','windows 7': '7.14.1.49','windows 8': 'not supported','windows 8.1': 'not supported', 'windows server 2008 R2': 'not supported'}, 
'view 4.6.3':{'windows xp': '11.6.0.39','windows vista': 'not supported','windows 7': '7.14.1.1052','windows 8': 'not supported','windows 8.1': 'not supported', 'windows server 2008 R2': 'not supported'}, 
'view 5.0':{'windows xp': '11.7.5.0','windows vista': '11.7.5.0','windows 7': '7.14.1.1061','windows 8': 'not supported','windows 8.1': 'not supported', 'windows server 2008 R2': 'not supported'}, 
'view 5.0.1':{'windows xp': '11.7.5.0','windows vista': '11.7.5.0','windows 7': '7.14.1.1063','windows 8': 'not supported','windows 8.1': 'not supported', 'windows server 2008 R2': 'not supported'}, 
'view 5.1.0':{'windows xp': '11.7.20.0','windows vista': 'not supported','windows 7': '7.14.1.1080','windows 8': 'not supported','windows 8.1': 'not supported', 'windows server 2008 R2': 'not supported'}, 
'view 5.1.1':{'windows xp': '11.6.0.35','windows vista': 'not supported','windows 7': 'not supported','windows 8': 'not supported','windows 8.1': 'not supported', 'windows server 2008 R2': 'not supported'}, 
'view 5.1.2':{'windows xp': '11.7.20.0','windows vista': 'not supported','windows 7': '7.14.1.1208','windows 8': 'not supported','windows 8.1': 'not supported', 'windows server 2008 R2': 'not supported'}, 
'view 5.1.3':{'windows xp': '11.7.20.0','windows vista': 'not supported','windows 7': '7.14.1.1.1208','windows 8': 'not supported','windows 8.1': 'not supported', 'windows server 2008 R2': 'not supported'}, 
'view 5.2.0':{'windows xp': '11.7.20.0','windows vista': 'not supported','windows 7': '7.14.1.1235','windows 8': '7.14.1.1235','windows 8.1': 'not supported', 'windows server 2008 R2': 'not supported'}, 
'view 5.3.0':{'windows xp': '12.0.23.0','windows vista': 'not supported','windows 7': '7.14.1.2021','windows 8': '7.14.1.2021','windows 8.1': '7.14.1.2021', 'windows server 2008 R2': '7.14.1.2021'}, 
'view 5.3.1':{'windows xp': '12.0.23.0', 'windows vista': 'not supported', 'windows 7': '7.14.1.2021','windows 8': '7.14.1.2021','windows 8.1': '7.14.1.2021', 'windows server 2008 R2': '7.14.1.2021'},
'view 6.0':{'windows xp': '12.0.23.0', 'windows vista': 'not supported', 'windows 7': '7.14.1.2032', 'windows 8': '7.14.1.2032', 'windows 8.1': '7.14.1.2032', 'windows server 20008 R2': '7.14.1.2032'}}
            if view_version in version_dict.keys:
                if os_name in version_dict[view_version].keys:
                    if version == version_dict[os_name]:
                        print 'The display driver version'+ version+ 'is compatible with the current'+view_version+'and'+os_name
                    else:
                        print 'The display driver version'+version+ 'is not compatible with the current'+view_version+ 'and'+ os_name

2 个答案:

答案 0 :(得分:0)

dict.keys是一个函数,而不是一个属性。因此所有这些检查都应该是

if view_version in version_dict.keys():
    ...

请注意keys后面的括号表示函数调用。

此外,我不知道这是否是复制粘贴错误或其他内容,但底部代码块的缩进是不正确的。

答案 1 :(得分:0)

您不需要致电.keys来检查包含在Python词典中的内容。你可以做到

if view_version in version_dict:
    if os_name in version_dict[view_version]:
        ...

在Python 2.x中,使用in dict.keys() 极其慢,因为它将构造整个键列表,然后执行list-contains测试(这需要遍历整个列表键);这完全放弃了首先使用字典的意义(快速查找)。所以,你绝对应该使用in dict