是否有一个“堆栈和地图"?与字典类似? [英] Is there a "stack and map" analogue with dicts?

查看:86
本文介绍了是否有一个“堆栈和地图"?与字典类似?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我最近不得不将字典键映射到评估问题中的值.我从以下内容开始:

I recently had to map dict keys to values in an assessment question. I started with the following:

files=
{'Code.py': 'Stan', 'Output.txt': 'Randy', 'Input.txt': 'Randy'}

然后将文件映射到它们的所有者,为此,我使用了以下内容:

And was to map the files to their owners, for which I used the following:

mapped={
        name:[key for key,value in files.items() if value==name]
        for name in list(set([value for key,value in files.items()]))
        }

mapped字典中,我给了我想要的东西:

Which gave me what I wanted in the mapped dict:

{'Stan': ['Code.py'], 'Randy': ['Output.txt', 'Input.txt']}

我只是想知道是否还有更多类似熊猫的方法来做同样的事情,但是使用简单的字典.

I was just wondering if there is a more Pandas-like way of doing the same thing, but with a plain dictionary.

推荐答案

您可以只使用defaultdict:

from collections import defaultdict
mapped = defaultdict(list)
​
for k, v in files.items():
    mapped[v].append(k)

mapped
# defaultdict(list, {'Stan': ['Code.py'], 'Randy': ['Output.txt', 'Input.txt']})

或在字典上使用setdefault方法:

mapped = {}
​
for k, v in files.items():
    mapped.setdefault(v, []).append(k)

mapped
# {'Stan': ['Code.py'], 'Randy': ['Output.txt', 'Input.txt']}


或者,如果您更喜欢pandas(但是对于此任务而言效率不高):


Or if you prefer pandas (which would not be as efficient for this task however):

s = pd.Series(files)
s.groupby(s).agg(lambda x: x.index.tolist()).to_dict()
# {'Randy': ['Input.txt', 'Output.txt'], 'Stan': ['Code.py']}


对小样本数据进行计时:


Timing on the small sample data:

%%timeit
from collections import defaultdict
mapped = defaultdict(list)
​
for k, v in files.items():
    mapped[v].append(k)
# 2 µs ± 33.3 ns per loop (mean ± std. dev. of 7 runs, 1000000 loops each)

%%timeit
s = pd.Series(files)
s.groupby(s).agg(lambda x: x.index.tolist()).to_dict()
# 2.12 ms ± 54.2 µs per loop (mean ± std. dev. of 7 runs, 100 loops each)

这篇关于是否有一个“堆栈和地图"?与字典类似?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆