字典中具有重复值的键

时间:2020-08-30 03:19:26

标签: python-3.x

说我有一本这样的字典:

using UnityEngine;
using System.Threading;
public class HungerSystem : MonoBehaviour
{
    public int hunger;
    public int hungerMultiplier;

    void Update() {
        if (hunger < 1000) {
            hunger = hunger + (1 * hungerMultiplier);
            Thread.Sleep(1000);
            Debug.Log("Me Hungory");
        }
    }
    
}

和 现在,如果我的searchQuery说“ eto”;在输出中获取相关的值

output

Q:但是如何获取该特定VALUE的所有键的列表 说如果searchQuery说“ enzomac”, 输出: enzomac forte ---> rack1;和 rack1-> ['etova ER400','enzomac forte','myosone']

我尝试过:

{'etova ER400': 'rack1', 'enzomac forte': 'rack1', 'myosone': 'rack1', 'etova 200': 'rack2', 'etogesic ER': 'rack2'}

1 个答案:

答案 0 :(得分:0)

我不知道我是否正确理解rev_dict所需要的内容,看来这只是完全相反的指示,这使我在查询中进行处理变得很奇怪,因为查询结果不需要反向命令但是,您可以在这里:

reverse_dict = {}
rev_dict = {}
flipped={}
myDict = {}

myDict.update(dict.fromkeys(['etova ER400', 'enzomac forte','myosone'], 'rack1'))
myDict.update(dict.fromkeys(['etova 200','etogesic ER'], 'rack2'))
search_key = input("Enter name of med : ").lower()
lowest_key = None
for key in myDict.keys():
    item = myDict[key]
    if key.startswith(search_key):
        print(key + "--->" + item)
        keyvalue = int(item[4:])
        if lowest_key is None or lowest_key > keyvalue:
            lowest_key = keyvalue
    if item not in reverse_dict.keys():
        reverse_dict[item] = []
    reverse_dict[item].append(key)

if lowest_key is not None:
    rack = 'rack' + str(lowest_key)
    rev_dict = {rack: reverse_dict[rack]}
    print(rev_dict)