根据其值来聚类dict的键 [英] Clustering the keys of a dict according to its values

查看:53
本文介绍了根据其值来聚类dict的键的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好!


给定一本字典,我想创建一个字典版本,

收集具有相同值的密钥:


>> d = {''a'':1,''b'' :2,''c'':1,'''':1,'''':2,''f'':3}
cluster(d)



{1:[''a'',''c'',''d''],2:[''b'',' 'e''],3:[''f'']}


也就是说,生成一个新的dict,它包含旧dict的每个值

a这个有很大价值的旧dict键的列表。


另一个要求是它也应该在列表上工作,在这种情况下

用索引代替键。我们可以假设

原始字典/列表中的所有值都可以用作字典键。


现在我这样做:


def cluster(d):

试试:

#是da dict?

值=唯一(d.values())

keys = d.keys()
除了AttributeError之外的


#假设d是一个列表

值=唯一(d)

键=范围(len(d))


clusters = {}

表示值:

clusters [value] = filter(lambda v:d [v] == value,keys)


return群集


其中unique()来自O''Reilly'的Python Cookbook,并返回一个列表

,其中包含给定列表中的每个项目一次。


现在我对Python很陌生,而且可能性很高,这可能会使得b
变得更漂亮和/或更高效。因此,我们非常感谢任何关于

以上代码的评论。

问候,

Florian

解决方案

Florian Brucker:


我们可以假设

原始字典/列表中的所有值都可以是用作字典键。



你确定吗?这是为了学校吗?


再见,

熊宝宝


你确定吗?这是为了学校吗?


是的,我很确定(原始字典的值是整数),并且

不,这不是'不上学:)如果我们可以假设...让你觉得

所以,我想这就是每个人在阅读了许多数学论文之后制定要求的方式:D


如果您对此感兴趣:我正在尝试重现某个dict键的某个排序的

版本(根据它们的值排序)

值不是唯一的,因此排序的版本不是唯一的,

。我原来排序的所有内容都是一些统计数据,所以我可以检查每个排序版本是否与原始版本相同。如果

我有聚类我正在谈论获得所有有效排序的

列表是连接集群排列的问题。

问候,

Florian


11月14日下午1:16,Florian Brucker< t ... @ torfbold.comwrote:


各位大家好!


鉴于字典,我想创建一个字典版本,

收集具有相同值的键:


>> d = {''a'':1,' 'b'':2,''c'':1,'''':1,'''':2,''f'':3}

>> ; cluster(d)



{1:[''a'',''c'','d''],2: [''b'',''e''],3:[''f'']}


也就是说,生成一个新的dict,它为每个值保存老字典

a列表中的键那个有很大价值的老字典。


另一个要求是它也应该在列表上工作,在这种情况下

带索引而不是键。我们可以假设

原始字典/列表中的所有值都可以用作字典键。


现在我这样做:


def cluster(d):

试试:

#是da dict?

值=唯一(d.values())

keys = d.keys()
除了AttributeError之外的


#假设d是一个列表

值=唯一(d)

键=范围(len(d))


clusters = {}

表示值:

clusters [value] = filter(lambda v:d [v] == value,keys)


return群集


其中unique()来自O''Reilly'的Python Cookbook,并返回一个列表

,其中包含给定列表中的每个项目一次。


现在我对Python很陌生,而且可能性很高,这可能会使得b
变得更漂亮和/或更高效。因此,我们非常感谢任何关于

以上代码的评论。


问候,

Florian



你的实现是非常低效的,因为它遍历

整个字典的次数与其中有不同的值一样多。

这是一个更简单的解决方案。


来自集合导入defaultdict

def cluster(d):

clusters = defaultdict(list )

表示密钥,val表示d.iteritems():

clusters [val] .append(密钥)

返回集群
或者没有defaultdict:


def cluster(d):

clusters = {}

for key, val in d.iteritems():

clusters.setdefault(val,[])。append(key)

return clusters

-

Arnaud


Hi everybody!

Given a dictionary, I want to create a clustered version of it,
collecting keys that have the same value:

>>d = {''a'':1, ''b'':2, ''c'':1, ''d'':1, ''e'':2, ''f'':3}
cluster(d)

{1:[''a'', ''c'', ''d''], 2:[''b'', ''e''], 3:[''f'']}

That is, generate a new dict which holds for each value of the old dict
a list of the keys of the old dict that have that very value.

Another requirement is that it should also work on lists, in that case
with indices instead of keys. We may assume that all values in the
original dict/list can be used as dict keys.

Right now I''m doing it like this:

def cluster(d):
try:
# is d a dict?
values = unique(d.values())
keys = d.keys()
except AttributeError:
# assume d is a list
values = unique(d)
keys = range(len(d))

clusters = {}
for value in values:
clusters[value] = filter(lambda v: d[v] == value, keys)

return clusters

where unique() is from O''Reilly''s Python Cookbook and returns a list
containing each item of the given list exactly once.

Now I''m pretty new to Python and chances are rather high that this could
be done prettier and/or more efficient. Therefore any comment on the
above code is greatly appreciated.
Regards,
Florian

解决方案

Florian Brucker:

We may assume that all values in the
original dict/list can be used as dict keys.

Are you sure? Is this for school?

Bye,
bearophile


Are you sure? Is this for school?

Yes, I''m pretty sure (the values of the original dict are integers), and
no, this isn''t for school :) If the "We may assume ..." made you think
so, I guess that''s the way everybody formulates requirements after
having read to many math papers :D

If it is of interest to you: I''m trying to reproduce a certain sorted
version of the keys of a dict (sorted according to their value) where
the values are not unique, and thus the sorted version is not unique,
either. All I have left of the original sorting is some statistics, so I
can check for every sorted version if it''s the same as the original. If
I have the clustering I''m talking about getting all the valid sorted
lists is a matter of concatenating permutations of the clusters.
Regards,
Florian


On Nov 14, 1:16 pm, Florian Brucker <t...@torfbold.comwrote:

Hi everybody!

Given a dictionary, I want to create a clustered version of it,
collecting keys that have the same value:

>>d = {''a'':1, ''b'':2, ''c'':1, ''d'':1, ''e'':2, ''f'':3}
>>cluster(d)

{1:[''a'', ''c'', ''d''], 2:[''b'', ''e''], 3:[''f'']}

That is, generate a new dict which holds for each value of the old dict
a list of the keys of the old dict that have that very value.

Another requirement is that it should also work on lists, in that case
with indices instead of keys. We may assume that all values in the
original dict/list can be used as dict keys.

Right now I''m doing it like this:

def cluster(d):
try:
# is d a dict?
values = unique(d.values())
keys = d.keys()
except AttributeError:
# assume d is a list
values = unique(d)
keys = range(len(d))

clusters = {}
for value in values:
clusters[value] = filter(lambda v: d[v] == value, keys)

return clusters

where unique() is from O''Reilly''s Python Cookbook and returns a list
containing each item of the given list exactly once.

Now I''m pretty new to Python and chances are rather high that this could
be done prettier and/or more efficient. Therefore any comment on the
above code is greatly appreciated.

Regards,
Florian

Your implementation is pretty inefficient as it iterates over the
whole dictionary as many times as there are distinct values in it.
Here is a simpler solution.

from collections import defaultdict

def cluster(d):
clusters = defaultdict(list)
for key, val in d.iteritems():
clusters[val].append(key)
return clusters
Or without defaultdict:

def cluster(d):
clusters = {}
for key, val in d.iteritems():
clusters.setdefault(val, []).append(key)
return clusters
--
Arnaud


这篇关于根据其值来聚类dict的键的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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