获取具有相同值的键 [英] get keys with the same values

查看:87
本文介绍了获取具有相同值的键的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好,


我有一本字典,会得到所有具有相同价值的键。


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


我的意思是:


d.keys(它们的值相同)


有了这个陈述,我可以得到两个这个例子的列表:

l1 = ['''',''e'']

l2 = [''b'',''d'']


有人会告诉我怎么做吗?


此致,

Nader

解决方案

Nader:


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

我的意思是:

d.keys(它们的值相同)



那是神奇的。


使用此语句,我可以获得此示例的两个列表:

l1 = ['''',''e'']

l2 = [''b'',''d'']

有人会告诉我怎么做吗?



您可以创建一个新的dict,其中键是输入的值

dict,值是一个键的列表原来的字典。所以

扫描键,输入dict的值,你可以填写第二个

dict。然后你可以扫描第二个字典,并创建一个列表,

只包含长于一个的值列表。


再见,

bearophile


2008年6月12日星期四03:58:53 -0700(PDT),Nader< n。***** @ gmail.com>

写道:


>你好,

我有一本字典,会得到所有具有相同字符的键价值。

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



这不是字典,而是'' sa语法错误。如果你实际上

有一本字典,你可以说


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


dd = {}


为密钥,值为d.items():

尝试:

dd [value] .append(key)
$除了KeyError之外的b $ b:

dd [value] = [key]


可能dd现在是你真正想要的;如果你真的想要你想要的话你可以使用


[l dd.values(l)l如果len(l)1]


>我会做的事情:

d.keys(它们的值相同)

这个陈述我可以得到这个例子的两个列表:
l1 = ['''',''e'']
l2 = [''b'',''d'']

有人会告诉我怎么做吗?

Nader



David C .Ullrich


6月12日下午1点35分,bearophileH ... @ lycos.com写道:


Nader:


d = {('''':1),(''b'':3),(''c'':2 ),('''':3),('''':1),(''f'':4)}

我会做的事情:

d.keys(它们的值相同)



这是神奇的。


使用此语句,我可以获得此示例的两个列表:

l1 = ['''',''e'']

l2 = [''b'',''d'']

有人会告诉我怎么做吗?



您可以创建一个新的dict,其中键是输入的值

dict,值是一个键的列表原来的字典。所以

扫描键,输入dict的值,你可以填写第二个

dict。然后你可以扫描第二个字典,并创建一个列表,

只包含长于一个的值列表。


再见,

bearophile



是否可以使用一个或两个语句,可能包含列表

comprehension。例如:


l = [(k,v)代表d.keys()中的k代表d.values()中的v在这里我们需要

一些额外的逻辑(v = 1)]


我不知道我们如何在列表中定义逻辑语句

理解。

如果可能的话它会非常紧凑。


Nader


Hello,

I have a dictionary and will get all keys which have the same values.

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

I will something as :

d.keys(where their values are the same)

With this statement I can get two lists for this example:
l1= [''a'',''e'']
l2=[''b'',''d'']

Would somebody tell me how I can do it?

Regards,
Nader

解决方案

Nader:

d = {(''a'' : 1), (''b'' : 3), (''c'' : 2),(''d'' : 3),(''e'' : 1),(''f'' : 4)}
I will something as :
d.keys(where their values are the same)

That''s magic.

With this statement I can get two lists for this example:
l1= [''a'',''e'']
l2=[''b'',''d'']
Would somebody tell me how I can do it?

You can create a new dict where the keys are the values of the input
dict and the values are a list of the keys of the original dict. So
scanning the keys, values of the input dict, you can fill the second
dict. Then you can scan the second dict, and create a list that
contains only value lists longer than one.

Bye,
bearophile


On Thu, 12 Jun 2008 03:58:53 -0700 (PDT), Nader <n.*****@gmail.com>
wrote:

>Hello,

I have a dictionary and will get all keys which have the same values.

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

That''s not a dictionary, it''s a syntax error. If you actually
have a dictionary you could say

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

dd = {}

for key, value in d.items():
try:
dd[value].append(key)
except KeyError:
dd[value] = [key]

Possibly dd is now what you really want; if you really
want what you said you want you could use

[l for l in dd.values() if len(l) 1]

>I will something as :

d.keys(where their values are the same)

With this statement I can get two lists for this example:
l1= [''a'',''e'']
l2=[''b'',''d'']

Would somebody tell me how I can do it?

Regards,
Nader

David C. Ullrich


On Jun 12, 1:35 pm, bearophileH...@lycos.com wrote:

Nader:

d = {(''a'' : 1), (''b'' : 3), (''c'' : 2),(''d'' : 3),(''e'' : 1),(''f'' : 4)}
I will something as :
d.keys(where their values are the same)


That''s magic.

With this statement I can get two lists for this example:
l1= [''a'',''e'']
l2=[''b'',''d'']
Would somebody tell me how I can do it?


You can create a new dict where the keys are the values of the input
dict and the values are a list of the keys of the original dict. So
scanning the keys, values of the input dict, you can fill the second
dict. Then you can scan the second dict, and create a list that
contains only value lists longer than one.

Bye,
bearophile

Is it niet possible with one or two statement, maybe with list
comprehension. For exmple:

l = [(k,v) for k in d.keys() for v in d.values() | en here we need
some extra logic (v = 1)]

I don;t konw how we can define a logic statement in a list
comprehension.
It will be very compact, if it would possible.

Nader


这篇关于获取具有相同值的键的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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