神奇的扩展哈希 [英] magical expanding hash
问题描述
我需要一个具有以下属性的神奇扩展哈希:
*它会创建所有中间键
meh [''foo' '] [''bar] = 1
- 即使meh [''foo'']之前也不存在
*允许将新元素推送到数组的叶子
meh [''foo''] [''list]<< elem1
meh [''foo''] [''list]<< elem2
*允许增加数字叶子
meh [''foo''] [''count''] + = 7
*可序列化
我在ruby中有这样的课程。 python能做到吗?
I need a magical expanding hash with the following properties:
* it creates all intermediate keys
meh[''foo''][''bar] = 1
-- works even if meh[''foo''] didn''t exist before
* allows pushing new elements to leaves which are arrays
meh[''foo''][''list] << elem1
meh[''foo''][''list] << elem2
* allows incrementing numeric leaves
meh[''foo''][''count''] += 7
* serializable
I have such a class in ruby. Can python do that?
推荐答案
braver写道:
我需要一个神奇的扩展哈希以下属性:
*它创建所有中间键
meh [''foo''] [''bar] = 1
- 作品
*允许将新元素推向数组的叶子
meh [''foo''] [''list]<< elem1
meh [''foo''] [''list]<< elem2
*允许递增数字叶子
meh [''foo''] [''count''] + = 7
*可序列化
我在红宝石中有这样一堂课。 python能做到吗?
I need a magical expanding hash with the following properties:
* it creates all intermediate keys
meh[''foo''][''bar] = 1
-- works even if meh[''foo''] didn''t exist before
* allows pushing new elements to leaves which are arrays
meh[''foo''][''list] << elem1
meh[''foo''][''list] << elem2
* allows incrementing numeric leaves
meh[''foo''][''count''] += 7
* serializable
I have such a class in ruby. Can python do that?
这太神奇了吗?
class meh(dict):
def __getitem __(self,item ):
if self.has_key(item):
return dict .__ getitem __(self,item)
else:
anitem = meh()
dict .__ setitem __(self,item,anitem)
返回anitem
m = meh()
m [''bob''] [''carol''] [''ted''] = 2
print m ['' bob''] [''carol''] [''ted'']
Is this too magical?
class meh(dict):
def __getitem__(self, item):
if self.has_key(item):
return dict.__getitem__(self, item)
else:
anitem = meh()
dict.__setitem__(self, item, anitem)
return anitem
m = meh()
m[''bob''][''carol''][''ted''] = 2
print m[''bob''][''carol''][''ted'']
" braver" <德********* @ gmail.com>写道:
"braver" <de*********@gmail.com> writes:
我需要一个具有以下属性的神奇扩展哈希:...
我在ruby中有这样一个类。 python可以这样做吗?
I need a magical expanding hash with the following properties: ...
I have such a class in ruby. Can python do that?
Python的内置dict对象不能这样做但你可以写这样的
a类很简单。
Python''s built-in dict objects don''t do that but you could write such
a class pretty straightforwardly.
James Stroud写道:
James Stroud wrote:
我需要一个神奇的扩展哈希以下属性:
*它创建所有中间键
meh [''foo''] [''bar] = 1
-
*允许将新元素推向数组的叶子
meh [''foo'''即使meh [''foo'']之前也不存在] [''list]<< elem1
meh [''foo''] [''list]<< elem2
*允许递增数字叶子
meh [''foo''] [''count''] + = 7
*可序列化
我在红宝石中有这样一堂课。 python能做到吗?
I need a magical expanding hash with the following properties:
* it creates all intermediate keys
meh[''foo''][''bar] = 1
-- works even if meh[''foo''] didn''t exist before
* allows pushing new elements to leaves which are arrays
meh[''foo''][''list] << elem1
meh[''foo''][''list] << elem2
* allows incrementing numeric leaves
meh[''foo''][''count''] += 7
* serializable
I have such a class in ruby. Can python do that?
这太神奇了吗?
class meh(dict):
def __getitem __(self,item):
如果self.has_key(item):
返回dict .__ getitem __(self,item)
否则:
anitem = meh()
dict .__ setitem __(self,item, anitem)
返回anitem
Is this too magical?
class meh(dict):
def __getitem__(self, item):
if self.has_key(item):
return dict.__getitem__(self, item)
else:
anitem = meh()
dict.__setitem__(self, item, anitem)
return anitem
实际上OP想要的是一种dict方法,它叫做
setdefault ()。它不会被[]重载。因为它被认为是更好的
能够说我想要自动生成。明确而非隐含:它b / b
为用户提供更强大的控制权,并执行更严格的规则。
Actually what the OP wants is already a method of dict, it''s called
setdefault(). It''s not overloaded by "[]" because it''s believed to be better to
be able to say "I want auto-generation" explicitally rather than implicitly: it
gives the user more power to control, and to enforce stricter rules.
class meh(dict):
.... def __getitem __(self,item):
.... return dict.setdefault(self,item,meh ())
.... a = meh()
a [" foo"] [" bar"] = 2
a [" foo"] [" dup"] = 3
打印一个[" foo"] [" bar"]
2打印一个
class meh(dict): .... def __getitem__(self, item):
.... return dict.setdefault(self, item, meh())
.... a = meh()
a["foo"]["bar"] = 2
a["foo"]["dup"] = 3
print a["foo"]["bar"] 2 print a
{''foo'':{''dup'':3,''bar'':2}}
所以我建议使用这个类,并建议OP尝试使用setdefault()
明确地更好地理解Python的哲学。
BTW:记住setdefault()写的是setdefault()。但它已经阅读了
" getorset()"。
-
Giovanni Bajo
{''foo'': {''dup'': 3, ''bar'': 2}}
So I advise using this class, and suggest the OP to try using setdefault()
explicitally to better understand Python''s philosophy.
BTW: remember that setdefault() is written "setdefault()" but it''s read
"getorset()".
--
Giovanni Bajo
这篇关于神奇的扩展哈希的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!