不区分大小写的字典,非破坏性,快速,任何人? [英] Case-insensitive dict, non-destructive, fast, anyone?

查看:90
本文介绍了不区分大小写的字典,非破坏性,快速,任何人?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要一个dict(好吧,无论如何都是最优的)类将

键存储为字符串而不强迫大写或更低,但仍然是

提供快速查找(即使用哈希表)。

d = CiDict([(''Hi'',12),( ''hoho'',13)])
d [''hi'']


12

d.keys()




[''嗨'',''hoho'']


注意'嗨''保留了这个案子。我想'Hi''和''hi''将需要共享相同的哈希值,以便查找速度快。


任何人都有我可以使用的实现?快速谷歌搜索

生成的实现将所有键强制为小写。


-

Ville Vainio http://tinyurl.com/2prnb

解决方案

Ville Vainio写道:

我需要一个dict(好吧,无论如何都是最优的)类,它将
键存储为字符串而不会将案例强制到上部或下部,但是仍然提供快速查找(即使用哈希表)。




将原始密钥与值一起存储并使用小写密钥

for lookup。


只有草图:


class MyDict:

def __init__(self):

self.inner = {}

def __setitem __(自我,键,值):

self.inner [key。 lower()] =(键,值)


def __getitem__(自我,键):

realkey,realvalue = self.inner [self]

返回realvalue


def get(自我,密钥,默认=无):

尝试:

返回自我[密钥]

除了KeyError:

返回默认值

#或:return self.inner.get(key.lower(),(None,默认))[1]

def键(个体经营):

返回[realkey for realkey,realval in self.inner.values()]


def值(自我):

返回[realkey for realkey,realval in self.inner.values]]


def items():

返回self.inner.values()


#iterators留作练习


>>>>> "丹尼尔" == Daniel Dittmar< da ************ @ sap.corp>写道:


Daniel> Ville Vainio写道:

我需要一个dict(好吧,无论如何都是最优的)类将
存储为字符串而不强制上层或下面的情况,但仍然提供快速查找(即使用哈希表)。




Daniel>将原始密钥与值一起存储并使用

Daniel>用于查找的小写键。


这就是我最初的想法,但字符串占据了dict中大部分的

空间而且我没有感觉就像加倍了。


这将是最简单的事情,可能会起作用,但是。


-

Ville Vainio http://tinyurl.com/2prnb


Ville Vainio写道:

>>" Daniel" == Daniel Dittmar< da ************ @ sap.corp>写道:

丹尼尔> Ville Vainio写道:
>>我需要一个dict(好吧,无论如何都是最佳的)类
>>将密钥存储为字符串而不将案例强制转换为上层
>>或更低,但仍然提供快速查找(即使用哈希
>>表)。



丹尼尔>将原始密钥与值一起存储并使用
Daniel>用于查找的小写键。

这就是我最初的想法,但字符串占据了dict的大部分空间,我不觉得它的大小加倍。




您可以编写一个更改比较和散列的字符串包装器。

我不确定这个(+ 1(Python对象+实例)字典))

比其他提议使用更少的内存(+ 1元组+ 1字符串)。


Daniel


I need a dict (well, it would be optimal anyway) class that stores the
keys as strings without coercing the case to upper or lower, but still
provides fast lookup (i.e. uses hash table).

d = CiDict([(''Hi'', 12),(''hoho'',13)])
d[''hi'']
12
d.keys()



[''Hi'',''hoho'']

Note that ''Hi'' preserved the case. I imagine that ''Hi'' and ''hi'' would
need to share the same hash value in order for the lookup to be fast.

Anyone have a an implementation that I could use? Quick googling only
produced implementations that coerce all keys to lowercase.

--
Ville Vainio http://tinyurl.com/2prnb

解决方案

Ville Vainio wrote:

I need a dict (well, it would be optimal anyway) class that stores the
keys as strings without coercing the case to upper or lower, but still
provides fast lookup (i.e. uses hash table).



Store the original key together with the value and use a lowercase key
for lookup.

only a sketch:

class MyDict:
def __init__ (self):
self.inner = {}

def __setitem__ (self, key, value):
self.inner [key.lower ()] = (key, value]

def __getitem__ (self, key):
realkey, realvalue = self.inner [self]
return realvalue

def get (self, key, default = None):
try:
return self [key]
except KeyError:
return default
# or: return self.inner.get (key.lower (), (None, default)) [1]

def keys (self):
return [realkey for realkey, realval in self.inner.values ()]

def values (self):
return [realval for realkey, realval in self.inner.values )]

def items ():
return self.inner.values ()

# iterators are left as an exercise


>>>>> "Daniel" == Daniel Dittmar <da************@sap.corp> writes:

Daniel> Ville Vainio wrote:

I need a dict (well, it would be optimal anyway) class that
stores the keys as strings without coercing the case to upper
or lower, but still provides fast lookup (i.e. uses hash
table).



Daniel> Store the original key together with the value and use a
Daniel> lowercase key for lookup.

That''s what I thought initially, but the strings take most of the
space in dict and I didn''t feel like doubling the size.

It would be the "simplest thing that could possibly work", though.

--
Ville Vainio http://tinyurl.com/2prnb


Ville Vainio wrote:

>>"Daniel" == Daniel Dittmar <da************@sap.corp> writes:

Daniel> Ville Vainio wrote:
>> I need a dict (well, it would be optimal anyway) class that
>> stores the keys as strings without coercing the case to upper
>> or lower, but still provides fast lookup (i.e. uses hash
>> table).



Daniel> Store the original key together with the value and use a
Daniel> lowercase key for lookup.

That''s what I thought initially, but the strings take most of the
space in dict and I didn''t feel like doubling the size.



You could write a string wrapper that changes comparison and hashing.
I''m not sure that this (+ 1 (Python object + instance dictionary)) would
use less memory than the other proposal (+ 1 Tuple + 1 String).

Daniel


这篇关于不区分大小写的字典,非破坏性,快速,任何人?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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