我如何检查变量是否在python中的字典中的列表中?

时间:2014-08-12 22:36:40

标签: python list loops python-2.7 dictionary

我正在编写一个程序来分配客户许可证。但是,每当更改数据库中的许可证时,都需要在另一个程序中更改它。但是我遇到了麻烦,因为我在字典中嵌套了一个列表,当我在字典中使用if时,即使我知道它确实存在,它也找不到它。

annotation = {'customer_id': 35, 'guest_os': 1287, 'license': [('VMware VM', 01), ('Veeam Backup VM', 02)]}

database_license = [('Veeam Backup VM', 02), ('VMware VM', 01)]

for product, license_id in annotation['license']:
    if license_id in database_license:
       print "do nothing"
    else:
       del annotation['license']
       annotation['license'] = database_license
       change = True

    if change == True:         
       annotation['license'] = license_check
       change_annotation(vm_mor, annotation)
       change = False    

由于某种原因,我似乎无法修复它将无法在列表database_licenses中找到值license_id,它只是执行其他操作,而不是不打印任何内容。

有什么想法吗?

我想使用in因为它们可能会出现故障,因此如果你遍历两者并使用if id == id id它将不会一直有效..

这是工作代码:

 if str(vm_mor) == vm['config.annotation']:
    annotation= pickle.load(open(str(vm_mor), "rb"))
    print annotation

    sql_check_exist = '''select a.vm_mor, b.license_id, c.product from vms a , vm_licenses b, licenses c where a.vm_id = b.vm_id and b.license_id = c.license_id and a.vm_mor = '%s' ''' % str(vm_mor) 
    cursor_exist.execute(sql_check_exist)
    database_license = []

    for vm_mor, license_id, product in cursor_exist:
       database_license.append((product,license_id))

    checklist_database_license = [int(i[1]) for i in database_license] #make a list of 2nd element of all the tuples in the database_license list
    check_me = annotation['license']

    for product, license_id in check_me:
       if license_id in checklist_database_license:
          print "do nothing"
       else:
          del annotation['license']
          annotation['license'] = database_license
          change = True

    if change == True:         
       change_annotation(vm_mor, annotation)
       change = False    

 else:
    print vm['config.name']
    pickle_mor(vm_mor,vm) 

3 个答案:

答案 0 :(得分:0)

annotation['license']是一个包含两个元组的列表:

>>> annotation = {'customer_id': 35, 'guest_os': 1287, 'license': [('VMware VM', 01), ('Veeam Backup VM', 02)]}
>>> aList = annotation['license']
>>> aList[0]
('VMware VM', 1)
>>> aList[1]
('Veeam Backup VM', 2)
>>> (product, license_id) = aList[0]
>>> product
'VMware VM'
>>> license_id
1
>>>

希望这能澄清情况。

答案 1 :(得分:0)

database_license = [('Veeam Backup VM', 02), ('VMware VM', 01)]
checklist_database_license = [int(i[1]) for i in database_license] #make a list of 2nd element of all the tuples in the database_license list

for product, license_id in annotation['license']:
    if license_id in checklist_database_license:
       print "do nothing"
    else:
       del annotation['license']
       annotation['license'] = database_license
       change = True

答案 2 :(得分:0)

您已将database_license定义为元组(对)的列表,因此无法使用35之类的字符串。

您要检查的是,是否有任何成员具有匹配的ID。像这样:

if any(product_license_id == license_id
       for product, license_id in database_license):

......或:

if product_license_id in map(itemgetter(1), database_license):

但也许最好将database_license存储为dict,将许可证ID映射到产品:

database_license = {2: 'Veeam Backup', 1: 'VMWare VM'}

然后你现有的代码就可以了。