[英]how do i check to see if a variable is inside a list housed in a dictionary in python?
I'm writing a program to assign customer licenses. 我正在编写一个分配客户许可证的程序。 However, whenever the license in the database is changed it needs to change it in another program. 但是,无论何时更改数据库中的许可证,都需要在另一个程序中进行更改。 However I'm having trouble because I nested a list inside of a dictionary and when I use if id in dictionary it doesn't find it even though I know its there for sure. 但是我遇到了麻烦,因为我在字典中嵌套了一个列表,当我在字典中使用if id时,即使我确定在那里也找不到它。
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
for some reason that I can't seem to fix it will not find the value license_id inside the list database_licenses it just does the else, instead of print nothing. 由于某种原因,我似乎无法修复它,因此在列表database_licenses内找不到值license_id,它只会执行其他操作,而不是不打印任何内容。
Any ideas? 有任何想法吗?
I want to use in because they could be out of order and therefor if you loop through both and use if ths id == that id it won't always work.. 我想使用in因为它们可能会乱序,因此如果您遍历两者并使用if id == that id它将永远无法工作。
This is the working code: 这是工作代码:
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)
annotation['license']
is a list containing two tuples: 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
>>>
Hope this clarifies the situation. 希望这可以澄清情况。
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
You've defined database_license
as a list of tuples (pairs), so there's no way a string like 35
can be in it. 您已将database_license
定义为元组(对)的列表,因此无法在其中输入35
类的字符串。
What you want to check is if any of the members have a matching id. 您要检查的是是否有任何成员具有匹配的ID。 Like this: 像这样:
if any(product_license_id == license_id
for product, license_id in database_license):
… or: … 要么:
if product_license_id in map(itemgetter(1), database_license):
But maybe it would be better to store database_license
as a dict
, mapping license IDs to products: 但是也许最好将database_license
作为dict
存储,将许可证ID映射到产品:
database_license = {2: 'Veeam Backup', 1: 'VMWare VM'}
Then your existing code would work just fine. 这样,您现有的代码就可以正常工作了。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.