表达形式的一对多词典? [英] expression form of one-to-many dict?

查看:51
本文介绍了表达形式的一对多词典?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我最终写了这样的代码:


map = {}

为关键,按顺序排列值:

map.setdefault(key,[])。append(value)


这段代码基本上构造了一对多的映射 - 每个值都是

a密钥发生时存储在该密钥的列表中。


这段代码很好,看起来很简单,但多亏了生成器

表达式,我有点被宠坏了。 ;)我喜欢能够做一对一的映射,如下所示:


dict(序列)


或更适合我的场景:


dict((get_key(item),get_value(item)项目顺序)

这里的重点是,有一个简单的序列或GE,我可以将b
扔到dict构造函数中,用我的一对一映射吐出一个dict。 />

是否有类似的表达形式会吐出我的一对多

映射?


Steve

So I end up writing code like this a fair bit:

map = {}
for key, value in sequence:
map.setdefault(key, []).append(value)

This code basically constructs a one-to-many mapping -- each value that
a key occurs with is stored in the list for that key.

This code''s fine, and seems pretty simple, but thanks to generator
expressions, I''m getting kinda spoiled. ;) I like being able to do
something like the following for one-to-one mappings:

dict(sequence)

or a more likely scenario for me:

dict((get_key(item), get_value(item) for item in sequence)

The point here is that there''s a simple sequence or GE that I can throw
to the dict constructor that spits out a dict with my one-to-one mapping.

Is there a similar expression form that would spit out my one-to-many
mapping?

Steve

推荐答案

[Steven Bethard]
[Steven Bethard]
所以我最终写了这样的代码:

map = {}
用于键,值按顺序排列:
map.setdefault(key,[])。append(value)

这段代码基本上构造了一个 - to-many mapping - 密钥出现的每个
值都存储在该密钥的列表中。

这段代码很好,而且看起来很简单,但多亏了发电机的表情,我有点被宠坏了。 ;)我喜欢能够做一些类似于以下内容的一对一映射:

dict(序列)

或更可能出现的情况我:

dict((get_key(item),get_value(item)项目顺序)

这里的重点是,我有一个简单的序列或GE可以使用我的一对一映射向dict构造函数抛出一个字典。


这是一个简单的序列,因为它''一个简单的任务。它甚至比它应该更简单,因为它任意决定,如果

多于一个键的一个实例是看来,它只会记住与该密钥的最后一个实例相关的

值。

是否有类似的表达形式会吐出我的一对一/> manymapping?
So I end up writing code like this a fair bit:

map = {}
for key, value in sequence:
map.setdefault(key, []).append(value)

This code basically constructs a one-to-many mapping -- each
value that a key occurs with is stored in the list for that key.

This code''s fine, and seems pretty simple, but thanks to generator
expressions, I''m getting kinda spoiled. ;) I like being able to do
something like the following for one-to-one mappings:

dict(sequence)

or a more likely scenario for me:

dict((get_key(item), get_value(item) for item in sequence)

The point here is that there''s a simple sequence or GE that I can
throw to the dict constructor that spits out a dict with my one-to-
one mapping.
It''s a simple sequence because it''s a simple task. It''s even simpler
than perhaps it "should be", since it arbitrarily decides that, if
more than one instance of some key is seen, it will only remember the
value associated with the last instance of that key.
Is there a similar expression form that would spit out my one-to-
manymapping?




2.4中有一个简单的单行(但不是非常简洁),如果你的钥匙完全是
下令:来自
d = dict((k,map(get_value,g))

for k,g in groupby (排序(序列,键= get_key),

键= get_key))

真正的目的是增加对你的欣赏>
原始拼写< 0.2 wink>。



There''s a straightforward one-liner in 2.4 (but not notably concise),
if your keys are totally ordered:

from itertools import groupby
d = dict((k, map(get_value, g))
for k, g in groupby(sorted(sequence, key=get_key),
key=get_key))

The real purpose of that is to increase your appreciation for your
original spelling <0.2 wink>.


Tim Peters写道:
Tim Peters wrote:
重点这里有一个简单的序列或GE,我可以把它扔给dict构造函数,用我的一对一的映射来吐出一个字典。
The point here is that there''s a simple sequence or GE that I can
throw to the dict constructor that spits out a dict with my one-to-
one mapping.



这是一个简单的序列,因为它是一项简单的任务。它甚至比应该更简单,因为它任意决定,如果看到某个键的多个实例,它只会记住
值与该键的最后一个实例相关联。


It''s a simple sequence because it''s a simple task. It''s even simpler
than perhaps it "should be", since it arbitrarily decides that, if
more than one instance of some key is seen, it will only remember the
value associated with the last instance of that key.




好​​点。这是一种任意的行为决定,虽然在大多数情况下是合理的,但在我的情况下是不可取的。在思考这个问题时,我想到一个解决方案是改变这种行为:



Good point. That''s sort of an arbitrary behavior decision, which, while
reasonable in most situations is not desirable in mine. In thinking
about this, it struck me that one solution is to change that behavior:

类OneToMany(object,UserDict.DictMixin):
.... def __init __(* args,** kwds):

.... self,args = args [ 0],args [1:]

.... self._dict = {}

.... self.update(* args,** kwds)

.... def __getitem __(自我,关键):

....如果不是键入self._dict:

... .self._dict [key] = []

....返回self._dict [key]

.... def __setitem __(self,key,value) :

.... self [key] .append(value)

.... def键(个体经营):

.. .. return self._dict.keys()

.... d = OneToMany((pow(i,13,4),i)for i in range(10))
d [0]
[0,2,4,6,8] d [1]
[1,5,9] d [3]
class OneToMany(object, UserDict.DictMixin): .... def __init__(*args, **kwds):
.... self, args = args[0], args[1:]
.... self._dict = {}
.... self.update(*args, **kwds)
.... def __getitem__(self, key):
.... if not key in self._dict:
.... self._dict[key] = []
.... return self._dict[key]
.... def __setitem__(self, key, value):
.... self[key].append(value)
.... def keys(self):
.... return self._dict.keys()
.... d = OneToMany((pow(i, 13, 4), i) for i in range(10))
d[0] [0, 2, 4, 6, 8] d[1] [1, 5, 9] d[3]



[3,7]

我将不得不考虑是否值得在这周围保持一个像

这样的课程...它可能会使这种数据工作

交互式简单...


感谢您的评论!


Steve


[3, 7]

I''ll have to think about whether it would be worth keeping a class like
this around... It might make working with this kind of data
interactively simpler...

Thanks for the comments!

Steve


史蒂文,


建议:命名任何变量

" map"是个不错的主意。当你这样做时,你就会破坏你调用Python的地图功能的能力。同样适用于list,str,

或任何其他内置函数。


如果你还没被咬过这就是你,我是。


Larry Bates


Steven Bethard写道:
Steven,

Suggestion: It is a bad idea to name any variable
"map". When you do, you destroy your ability to call
Python''s map function. Same goes for "list", "str",
or any other built-in function.

If you haven''t been bitten by this you will, I was.

Larry Bates

Steven Bethard wrote:
所以我写完了像这样的代码:

map = {}
用于键,序列值:
map.setdefault(key,[])。append(value)
此代码'很好,看起来很简单,但多亏了发电机的表达方式,我有点被宠坏了。 ;)我喜欢能够做一些类似于以下内容的一对一映射:

dict(序列)

或更可能出现的情况我:

dict((get_key(item),get_value(item)项目顺序)

这里的重点是,我有一个简单的序列或GE可以投掷到用我的一对一映射吐出dict的dict构造函数。

是否有类似的表达形式会吐出我的一对多
映射?

史蒂夫
So I end up writing code like this a fair bit:

map = {}
for key, value in sequence:
map.setdefault(key, []).append(value)

This code basically constructs a one-to-many mapping -- each value that
a key occurs with is stored in the list for that key.

This code''s fine, and seems pretty simple, but thanks to generator
expressions, I''m getting kinda spoiled. ;) I like being able to do
something like the following for one-to-one mappings:

dict(sequence)

or a more likely scenario for me:

dict((get_key(item), get_value(item) for item in sequence)

The point here is that there''s a simple sequence or GE that I can throw
to the dict constructor that spits out a dict with my one-to-one mapping.

Is there a similar expression form that would spit out my one-to-many
mapping?

Steve



这篇关于表达形式的一对多词典?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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