【每日一题1019】字典对调

:mage:t2:给定一个键值都是字符串的字典,请编写一个函数,将原来的键和值进行对调。

示例
输入:{'Ice': 'Cream', 'Age': '21', 'Light': 'Cream', 'Double': 'Cream'}
输出:{'Cream': ['Ice', 'Double', 'Light'], '21': ['Age']}

题目难度:简单
题目来源:codewars : Swap items in a dictionary

def switch_dict(dc: dict) -> dict:
    pass

data = {'Ice': 'Cream', 'Age': '21', 'Light': 'Cream', 'Double': 'Cream'}
expected = {'Cream': ['Ice', 'Double', 'Light'], '21': ['Age']}

assert switch_dict(data) == expected
def switch_dict(dc: dict) -> dict:
    temp_dict={}
    for k,v in dc.items():
        temp_dict.setdefault(v, []).append(k)
    return temp_dict

data = {'Ice': 'Cream', 'Age': '21', 'Light': 'Cream', 'Double': 'Cream'}
expected = {'Cream': ['Ice', 'Double', 'Light'], '21': ['Age']}
assert switch_dict(data) == expected
1 Like
def switch_dict(dc: dict) -> dict:
    result = {}
    for val in set(dc.values()):
        result[val] = [key for key in dc.keys() if dc[key] == val]
    return result

data = {'Ice': 'Cream', 'Age': '21', 'Light': 'Cream', 'Double': 'Cream'}
expected = {'Cream': ['Ice', 'Light', 'Double'], '21': ['Age']}

assert switch_dict(data) == expected

expectdata Double 和 Light 的位置做了调整,没 get 到题目中为啥那样写 :joy:

字典是无序的,所以列表也是最后顺序是不确定的

def switch_dict(dc: dict) -> dict:
    swap_dc = {}
    for value, key in dc.items():
        print(key, value)
        if key in swap_dc.keys():
            if type(swap_dc[key]) is list:
                swap_dc[key].append(value)
            else:
                swap_dc[key] = [swap_dc[key], value]
        else:
            swap_dc[key] = [value]
    print(swap_dc)
    return swap_dc


data = {'Ice': 'Cream', 'Age': '21', 'Light': 'Cream', 'Double': 'Cream'}
expected = {'Cream': ['Ice', 'Double', 'Light'], '21': ['Age']}
1 Like
def switch_dict(dc):
    result = {}
    for key, value in dc.items():
        if value not in result.keys():
            result[value] = [key]
        else:
            result[value].append(key)
    return result

data = {'Ice':'Cream','Age':'21','Light':'Cream','Double':'Cream'}
expected = {'Cream':['Ice','Light','Double'],'21':['Age']}

assert switch_dict(data) == expected

+1

def switch_dict(dc: dict) -> dict:
    new_dict = {}
    new_list = []
    for k,v in dc.items():
        n = list(dc.values())
        if n.count(dc[k]) != 1:
            new_list.append(k)
            new_dict[v] = new_list
        else:
            new_dict[v] = [k]
    print(new_dict)
    return new_dict



data = {'Ice': 'Cream', 'Age': '21', 'Light': 'Cream', 'Double': 'Cream'}
expected = {'Cream': ['Ice', 'Light', 'Double'], '21': ['Age']}

assert switch_dict(data) == expected
public static HashMap<String,ArrayList<String>> Solution (HashMap<String,String>  inputMap){
        if (inputMap==null||inputMap.size()==0)  return null;
        HashMap<String,ArrayList<String>> result= new HashMap<>();
        for(String key:inputMap.keySet()){
            String value = inputMap.get(key);
            if (result.containsKey(value)){
                ArrayList<String> oldKeys = result.get(value);
                oldKeys.add(key);
                result.put(value,oldKeys);
            }else{
                ArrayList<String> keys = new ArrayList<>();
                keys.add(key);
                result.put(value,keys);
            }
        }
        return result;
    }
def switch_dict(dc: dict) -> dict:
    value=dc.values()
    result={}
    for val in value:
        result[val]=[i for i in dc.keys() if dc[i]==val]
    return result
data = {'Ice': 'Cream', 'Age': '21', 'Light': 'Cream', 'Double': 'Cream'}
expected = {'Cream': ['Ice', 'Light', 'Double'], '21': ['Age']}
assert switch_dict(data) == expected
def switch_dict(dc:dict):
    res={}
    for i in dc.keys():
        if dc[i] in res:
            res[dc[i]].append(i)
        else:
            li = []
            li.append(i)
            res[dc[i]] = li
    return res
关闭