条件列表元素值

时间:2019-04-04 19:23:53

标签: python

我想制作一个字典来比较两个值,看看它们是否相等。由于出现语法错误,您将如何执行此操作(请参阅compareDict的值-列表的第三元素)。 有没有更优雅的方法可以做到这一点?

def script_sanityCheck(shotDict):
    #retrieve data from current script
    script_frange = (nuke.root()['first_frame'].value(), nuke.root()['last_frame'].value())
    script_fps = nuke.root()['fps'].value()
    script_projectname = nuke.root()['project'].value()
    script_code = nuke.root()['shotcode'].value()
    script_neutralgrade = nuke.root()['neutralgrade_cdl'].value()

    #extract data from dictionary
    sg_frange = (shotDict['sg_cut_in'], shotDict['sg_cut_out'])
    sg_fps = shotDict['sg_fps']
    sg_projectname = shotDict['project.Project.name']
    sg_code = shotDict['code']
    sg_neutralgrade = shotDict['sg_neutralgrade_cdl']

    #compare both 
    compareDict = {
        'frange' : [script_frange, sg_frange, True if script_frange == sg_frange else False]
        'fps' : [script_fps, sg_fps, True if script_fps == sg_fps else False]
        'projectname' : [script_projectname, sg_projectname, True if script_projectname == sg_projectname else False ]
        'code' : [script_code, sg_code, True if script_code == sg_code else False]
        'neutralgrade' : [script_neutralgrade, sg_neutralgrade, True if script_neutralgrade == sg_neutralgrade else False]
    }
pprint(script_sanityCheck(shotDict))

1 个答案:

答案 0 :(得分:1)

从一开始就使用字典来组织数据。这样,您就可以使用dict理解来建立比较:

def script_sanityCheck(shotDict):
    #retrieve data from current script
    script = {'frange': (nuke.root()['first_frame'].value(), nuke.root()['last_frame'].value()),
              'fps':  nuke.root()['fps'].value(),
              'projectname': = nuke.root()['project'].value(),
              'code': nuke.root()['shotcode'].value(),
              'neutralgrade': nuke.root()['neutralgrade_cdl'].value()}

    #extract data from dictionary
    sg = {'frange': (shotDict['sg_cut_in'], shotDict['sg_cut_out']),
          'fps': shotDict['sg_fps'],
          'projectname': shotDict['project.Project.name'],
          'code': shotDict['code'],
          'neutralgrade': shotDict['sg_neutralgrade_cdl']}

    #compare both 
    compareDict = {key: [script[key], sg[key], script[key] == sg[key]] for key in sg} 
    return compareDict

pprint(script_sanityCheck(shotDict))

请注意,True if a == b else False就是a == b