龙空技术网

Python dict在items中根据value查找key

洪较瘦不着调退役it人 136

前言:

此时姐妹们对“python的items”大约比较看重,各位老铁们都想要分析一些“python的items”的相关内容。那么小编同时在网络上网罗了一些关于“python的items””的相关知识,希望看官们能喜欢,大家快快来了解一下吧!

Python字典中根据values查找key

6 分钟阅读

Python 字典中按值搜索键的技术

dict = {"key1": value1, "key2":value2, ...}

它是一种哈希映射,也可以在其他高级编程语言(如C++和Java)中使用。

Python 程序 – 在字典中搜索键

程序有一本财富 500 强公司及其世界排名:

# Dictionary of fortune 500 companiesdictOfFortune500 = {    "Walmart": 1,    "Exxon Mobil" : 2,    "Berkshire Hathaway" : 3,    "Apple" : 4,    "UnitedHealth Group" : 5,    "McKesson" : 5,    "CVS Health" : 5,    "Amazon.com" : 6,    "AT&T" : 6,    "General Motors" : 7    }

在字典中搜索世界排名第 5 的键。有三家公司占据第 5 位。

    "UnitedHealth Group" : 5,    "McKesson" : 5,    "CVS Health" : 5,

在字典中按值搜索键

字典对象有一个 items() 方法,该方法返回所有项目及其值的列表,即以键对的形式。

示例代码

它定义了一个函数,用于在财富 500 强公司的字典中搜索键。searchKeysByVal()

dictOfFortune500 = {    "Walmart": 1,    "Exxon Mobil" : 2,    "Berkshire Hathaway" : 3,    "Apple" : 4,    "UnitedHealth Group" : 5,    "McKesson" : 5,    "CVS Health" : 5,    "Amazon.com" : 6,    "AT&T" : 6,    "General Motors" : 7    }def searchKeysByVal(dict, byVal):    keysList = []    itemsList = dict.items()    for item in itemsList:        if item[1] == byVal:            keysList.append(item[0])    return keysList    keysList = searchKeysByVal(dictOfFortune500, 5) print("Fortune 500 Companies having world raking '5' are:", end = "\n\n")for index, company in enumerate(keysList):    print("{}: {}".format(index, company))

Result...Fortune 500 Companies having world raking '5' are:0: UnitedHealth Group1: McKesson2: CVS HealthCPU Time: 0.03 sec(s), Memory: 8392 kilobyte(s)

使用了Python for loop。

keysList = [company  for (company, value) in dictOfFortune500.items() if value == 5]print("Fortune 500 Companies having world raking '5' are:", end = "\n\n")#Iterate over the list of companiesfor index, company in enumerate(keysList):    print("{}: {}".format(index, company))

按值列表搜索字典中的键

我们将找出其值匹配的键

[5, 6]
def searchKeysByValList(itemDict, valList):    keysList = []    itemsList = itemDict.items()    for item  in itemsList:        if item[1] in valList:            keysList.append(item[0])    return  keysList

keysList = searchKeysByValList(dictOfFortune500, [5, 6] ) #Iterate over the list of valuesfor key in keysList:    print(key)

输出

UnitedHealth GroupMcKessonCVS HealthAmazon.comAT&T
合并整个代码
 dictOfFortune500 = {    "Walmart": 1,    "Exxon Mobil" : 2,    "Berkshire Hathaway" : 3,    "Apple" : 4,    "UnitedHealth Group" : 5,    "McKesson" : 5,    "CVS Health" : 5,    "Amazon.com" : 6,    "AT&T" : 6,    "General Motors" : 7    } def searchKeysByVal(dict, byVal):    keysList = []    itemsList = dict.items()    for item in itemsList:        if item[1] == byVal:            keysList.append(item[0])    return keysList     def searchKeysByValList(itemDict, valList):    keysList = []    itemsList = itemDict.items()    for item  in itemsList:        if item[1] in valList:            keysList.append(item[0])    return  keysList  keysList = searchKeysByVal(dictOfFortune500, 5) print("Fortune 500 Companies having world raking '5' are:", end = "\n\n")for index, company in enumerate(keysList):    print("{}: {}".format(index, company)) keysList = searchKeysByValList(dictOfFortune500, [5, 6] )print("\nFortune 500 Companies having world raking '5, 6' are:", end = "\n\n")#Iterate over the list of companiesfor index, company in enumerate(keysList):    print("{}: {}".format(index, company))

输出

Fortune 500 Companies having world raking '5' are:0: UnitedHealth Group1: McKesson2: CVS HealthFortune 500 Companies having world raking '5, 6' are:0: UnitedHealth Group1: McKesson2: CVS Health3: Amazon.com4: AT&T

标签: #python的items #python判断字典存在某个key