簡體   English   中英

如何在Python中的List字典字典中找到一個值?

[英]How to find a value in List of dictionary of dictionary of List in Python?

我對Python很陌生。 我必須查找/比較一個帶有列表的字符串值,該列表存在於List字典的字典列表中。 我寫了下面的代碼,雖然可以正常工作,但是我們可以用更好的方式編寫代碼。

abc = [{'GetDriverPackInfo_OUTPUT': {'OSList': [u'Linux', u'Windows', u'Xen', u'VMware'], 'ReturnValue': [u'0'], 'Version': [u'15.07.04']}}]

os_name = "Linux"

for k in abc:                   #['GetDriverPackInfo_OUTPUT']['OSList']:
    if os_name in k['GetDriverPackInfo_OUTPUT']['OSList']: #== os_name:
        print ("os_name found")
    else:
        print ("os_name not found")

我不確定這對您有用。 僅當abc始終有一個'GetDriverPackInfo_OUTPUT'項目時,該選項才起作用。

temp = abc [0] ['GetDriverPackInfo_OUTPUT'] ['OSList']

如果temp中的os_name:

  print "found"

其他:

  print "not found" 
In [1]: osname in [j for j in [k['GetDriverPackInfo_OUTPUT']['OSList'] for k in abc]][0]
Out[1]: True

如果返回True,則osname退出,否則退出。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM