无壳字典howto? [英] caseless dictionary howto ?

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

问题描述

你好,


我需要搜索一段文字并使所有相等的单词

(除了他们的情况)在他们的情况下也相同,基于第一次出现。

所以我使用字典存储对象的名称和属性。

因为我需要搜索无壳名称(所以我选择了小写字母),

我的dictionairy看起来像这样:


self.procs [" serial_hw_read" ] =(" Serial_HW_Read"," F"," ++",T)


这真的是一个很好的解决方案,

或还有更好的办法吗?


谢谢,

Stef Mientki

解决方案

2007年6月19日,Stef Mientki< S. ************** @ mailbox.kun.nlwrote:


你好,


我需要搜索一段文字并使所有相同的单词

(除了他们的情况)在他们的情况下也是相同的,基于第一次出现。

所以我用字典来存储对象的名称和属性。

因为我需要搜索无壳的名字(所以我' '选择小写字母),

我的字典看起来像这样:


self.procs [" serial_hw_read" ] =(" Serial_HW_Read"," F"," ++",T)


这真的是一个很好的解决方案,

或有更好的方法吗?



因为你想要一个差不多的字典,你应该创建一个新的类

继承自dict并重载你想要的方法

更改。


-

Evan Klitzke< ev ** @ yelp.com>


Evan Klitzke写道:


2007年6月19日,Stef Mientki< S. ********* *****@mailbox.kun.nlwrote:


> hello,

我需要搜索一段文字并制作所有相同的单词(除了他们的情况)在他们的情况下也是相同的,基于第一次出现。
所以我使用字典来存储对象的名称和属性。
因为我需要搜索无壳的名称(所以我选择了小写),
我的字典看起来像这样:

self.procs [" serial_hw_read" ] =(" Serial_HW_Read"," F"," ++",T)

这真的是一个很好的解决方案,
还是有更好的方法?



既然你想要一个几乎字典,你应该创建一个新的类

继承自dict并重载你想要的方法

更改。



谢谢,

但还有一个问题。


假设我成功创建了自己的类型,派生自字典,

我可以添加/设置这样的键:


self。 procs [" Serial_HW_Read" ] =(F,++,T)


现在我可以查询字典了


self .procs.has_key(" serial_hw_read")


但是如何获取原始字符串Serial_HW_Read?回来了?


谢谢,

Stef Mientki


Stef Mientki写道:
< blockquote class =post_quotes>
Evan Klitzke写道:


> 2007年6月19日,Stef Mientki< S. **** **********@mailbox.kun.nlwrote:


>> hello,

我需要根据第一次出现的情况,搜索一段文字并使所有相等的单词(除了他们的情况)在他们的情况下也相等。
所以我正在使用字典来存储对象的名称和属性。
因为我需要搜索无壳的名称(所以我选择了小写),
我的dictionairy看起来像这样:

自我.procs [" serial_hw_read" ] =(" Serial_HW_Read"," F",
" ++",T)

这真的是一个很好的解决方案,
还是有更好的方法?


因为你想要一个几乎字典,你应该创建一个继承自dict的新类
并重载你想要改变的方法。



假设我成功创建了自己的类型,派生自字典,

我可以像这样添加/设置一个键:


self.procs [" Serial_HW_Read" ] =(F,++,T)


现在我可以查询字典了


self .procs.has_key(" serial_hw_read")


但是如何获取原始字符串Serial_HW_Read?背部 ?



我可能不会继承dict - 你需要

无论如何重新定义大多数方法。我会做类似::


>> class D(object) :



.... def __init __(self):

.... self._dict = {}

.... self._original_keys = {}

.... def __iter __(self):

... 。关于self._dict:

....产生self._original_keys [key]

.... def __contains __(self,key):

....在self._dict中返回key.lower()

.... def __getitem __(自我,密钥):

... .return self._dict [key.lower()]

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

.... self._dict [ key.lower()] = value

....如果密钥不在self._original_keys中:

.... self._original_keys [key.lower()] = key

....


>> procs = D()
procs [''Serial_HW_Read''] =''F'',''+ +'','T''
''serial_hw_read''在procs



True
< blockquote class =post_quotes>


>> procs [''SErial_Hw_reAD'']



(''''',''++'',''T'')


>> list(procs)



[''序列号_HW_Read'']


请注意,因为我存储了每个键遇到的第一个表单,所以
总是可以使用小写版本来检索 ;原始字符串。


STeVe


hello,

I need to search a piece of text and make all words that are equal
(except their case) also equal in their case, based on the first occurrence.
So I''m using a dictionary to store names and attributes of objects.
As as I need to search on the caseless name (so I''ve choosen lowercase),
My dictionairy looks like this:

self.procs [ "serial_hw_read" ] = ( "Serial_HW_Read", "F", "++", T)

Is this really a good solution,
or are there better ways ?

thanks,
Stef Mientki

解决方案

On 6/19/07, Stef Mientki <S.**************@mailbox.kun.nlwrote:

hello,

I need to search a piece of text and make all words that are equal
(except their case) also equal in their case, based on the first occurrence.
So I''m using a dictionary to store names and attributes of objects.
As as I need to search on the caseless name (so I''ve choosen lowercase),
My dictionairy looks like this:

self.procs [ "serial_hw_read" ] = ( "Serial_HW_Read", "F", "++", T)

Is this really a good solution,
or are there better ways ?

Since you want an almost dictionary, you should create a new class
that inherits from dict and overloads the methods that you want to
change.

--
Evan Klitzke <ev**@yelp.com>


Evan Klitzke wrote:

On 6/19/07, Stef Mientki <S.**************@mailbox.kun.nlwrote:

>hello,

I need to search a piece of text and make all words that are equal
(except their case) also equal in their case, based on the first
occurrence.
So I''m using a dictionary to store names and attributes of objects.
As as I need to search on the caseless name (so I''ve choosen lowercase),
My dictionairy looks like this:

self.procs [ "serial_hw_read" ] = ( "Serial_HW_Read", "F", "++", T)

Is this really a good solution,
or are there better ways ?


Since you want an almost dictionary, you should create a new class
that inherits from dict and overloads the methods that you want to
change.

thanks,
but still one question.

Suppose I succeed in creating my own type, derived from dictionary,
I would be able to add/set a key like this:

self.procs [ "Serial_HW_Read" ] = ( "F", "++", T)

and now I can query the dictionary by

self.procs.has_key ( "serial_hw_read")

but how do I get the original string "Serial_HW_Read" back ?

thanks,
Stef Mientki


Stef Mientki wrote:

Evan Klitzke wrote:

>On 6/19/07, Stef Mientki <S.**************@mailbox.kun.nlwrote:

>>hello,

I need to search a piece of text and make all words that are equal
(except their case) also equal in their case, based on the first
occurrence.
So I''m using a dictionary to store names and attributes of objects.
As as I need to search on the caseless name (so I''ve choosen lowercase),
My dictionairy looks like this:

self.procs [ "serial_hw_read" ] = ( "Serial_HW_Read", "F",
"++", T)

Is this really a good solution,
or are there better ways ?


Since you want an almost dictionary, you should create a new class
that inherits from dict and overloads the methods that you want to
change.


Suppose I succeed in creating my own type, derived from dictionary,
I would be able to add/set a key like this:

self.procs [ "Serial_HW_Read" ] = ( "F", "++", T)

and now I can query the dictionary by

self.procs.has_key ( "serial_hw_read")

but how do I get the original string "Serial_HW_Read" back ?

I probably wouldn''t inherit from dict -- you''re going to need to
redefine most of the methods anyway. I''d do something like::

>>class D(object):

.... def __init__(self):
.... self._dict = {}
.... self._original_keys = {}
.... def __iter__(self):
.... for key in self._dict:
.... yield self._original_keys[key]
.... def __contains__(self, key):
.... return key.lower() in self._dict
.... def __getitem__(self, key):
.... return self._dict[key.lower()]
.... def __setitem__(self, key, value):
.... self._dict[key.lower()] = value
.... if key not in self._original_keys:
.... self._original_keys[key.lower()] = key
....

>>procs = D()
procs[''Serial_HW_Read''] = ''F'', ''++'', ''T''
''serial_hw_read'' in procs

True

>>procs[''SErial_Hw_reAD'']

(''F'', ''++'', ''T'')

>>list(procs)

[''Serial_HW_Read'']

Note that because I store the first form encountered for every key, I
can always use the lower-case version to retrieve the "original string".

STeVe


这篇关于无壳字典howto?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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