失踪?字典方法 [英] missing? dictionary methods

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

问题描述

至少我发现他们失踪了。


目前我经常遇到以下情况。


1)两个档案,每个都有同一个字典的键值对。

但是如果第二个文件包含一个

不在第一个文件中的键,则会出错。 />

在处理第二个文件时我错过了''set''方法。

dct.set(key,value)与dct [key] =相等值,

,但是如果键已经不在字典中,那么它会引发KeyError。

2)一个文件键值对。但是如果一个密钥在文件中重复则是一个错误




在处理这些文件时我错过了''make''方法。 />
dct.make(key,value)将等同于dct [key] = value。

除了如果键是
$ b它会引发KeyError $ b已经在字典里了。

其他人怎么想这个?


-

Antoon Pardon

Well at least I find them missing.

For the moment I frequently come across the following cases.

1) Two files, each with key-value pairs for the same dictionary.
However it is an error if the second file contains a key that
was not in the first file.

In treating the second file I miss a ''set'' method.
dct.set(key, value) would be equivallent to dct[key] = value,
except that it would raise a KeyError if the key wasn''t
already in the dictionary.
2) One file with key-value pairs. However it is an error
if a key is duplicated in the file.

In treating such files I miss a ''make'' method.
dct.make(key, value) would be equivallent to dct[key] = value.
except that it would raise a KeyError if the key was
already in the dictionary.
What do other people think about this?

--
Antoon Pardon

推荐答案

" Antoon Pardon" < AP ***** @ forel.vub.ac.be>在消息中写道

news:sl ******************** @ rcpc42.vub.ac.be ...
"Antoon Pardon" <ap*****@forel.vub.ac.be> wrote in message
news:sl********************@rcpc42.vub.ac.be...
至少我发现它们不见了。

目前我经常遇到以下情况。

1)两个文件,每个文件都有键值对相同的字典。
但是如果第二个文件包含
不在第一个文件中的密钥则会出错。

在处理第二个文件时我错过了一个''设置''方法。
dct.set(key,value)与dct [key] = value,
等效,但如果键不是
会引发KeyError已经在字典中。

2)一个具有键值对的文件。但是,如果文件中的密钥重复,则会出现错误。

在处理此类文件时,我会错过''make''方法。
dct.make(密钥,值)与dct [key] = value等效。
如果键已经在字典中,它会引发KeyError。

其他人怎么想关于这个?

-
Antoon Pardon
Well at least I find them missing.

For the moment I frequently come across the following cases.

1) Two files, each with key-value pairs for the same dictionary.
However it is an error if the second file contains a key that
was not in the first file.

In treating the second file I miss a ''set'' method.
dct.set(key, value) would be equivallent to dct[key] = value,
except that it would raise a KeyError if the key wasn''t
already in the dictionary.
2) One file with key-value pairs. However it is an error
if a key is duplicated in the file.

In treating such files I miss a ''make'' method.
dct.make(key, value) would be equivallent to dct[key] = value.
except that it would raise a KeyError if the key was
already in the dictionary.
What do other people think about this?

--
Antoon Pardon




+1


我我确定过去我需要并实现这个功能,但它很简单,即使你想要将它们提取到函数/方法中也是如此。与最近关于dict

累积方法的前PEP相比,set()和make()(或者他们可能被称为的任何东西)对于所有

dicts都是有意义的,因此,它们是被添加到基础词类的好候选者。


至于命名,我建议使用reset()而不是set(),以强调键必须在那里。

make()没关系;其他候选人可以是add()或put()。


George



+1

I''m sure I''ve needed and implemented this functionality in the past, but it was simple enough to
even think of extracting them into functions/methods. In contrast to the recent pre-PEP about dict
accumulating methods, set() and make() (or whatever they might be called) are meaningful for all
dicts, so they''re good candidates for being added to the base dict class.

As for naming, I would suggest reset() instead of set(), to emphasize that the key must be there.
make() is ok; other candidates could be add() or put().

George


Antoon Pardon写道:
Antoon Pardon wrote:
至少我发现它们不见了。

目前我经常遇到以下情况。

1)两个文件,每个文件都有键值对相同的字典。
但是如果第二个文件包含
不在第一个文件中的密钥则会出错。

在处理第二个文件时我错过了一个''集''方法。
dct.set(键,值)与dct [key] = value,
等效,但如果键已经没有它会引发KeyError在字典中。

2)一个具有键值对的文件。但是,如果文件中的密钥重复,则会出现错误。

在处理此类文件时,我会错过''make''方法。
dct.make(密钥,值)与dct [key] = value等效。
如果键已经在字典中,它会引发KeyError。

其他人怎么想关于这个?
Well at least I find them missing.

For the moment I frequently come across the following cases.

1) Two files, each with key-value pairs for the same dictionary.
However it is an error if the second file contains a key that
was not in the first file.

In treating the second file I miss a ''set'' method.
dct.set(key, value) would be equivallent to dct[key] = value,
except that it would raise a KeyError if the key wasn''t
already in the dictionary.
2) One file with key-value pairs. However it is an error
if a key is duplicated in the file.

In treating such files I miss a ''make'' method.
dct.make(key, value) would be equivallent to dct[key] = value.
except that it would raise a KeyError if the key was
already in the dictionary.
What do other people think about this?




def safeset(dct,key,value):

如果密钥不在dct中:

引发KeyError(键)

else:

dct [key] = value


def make(dct,key,value ):

如果在dct中键入:

引发KeyError('%r已经存在于dict''%键)

else:

dct [key] = value


我没有理由将这些内置于dict类型中。


-

Robert Kern
rk***@ucsd.edu


在草地长得高的地狱里

梦想的坟墓是否允许死亡。

- Richard Harter



def safeset(dct, key, value):
if key not in dct:
raise KeyError(key)
else:
dct[key] = value

def make(dct, key, value):
if key in dct:
raise KeyError(''%r already in dict'' % key)
else:
dct[key] = value

I don''t see a good reason to make these built in to dict type.

--
Robert Kern
rk***@ucsd.edu

"In the fields of hell where the grass grows high
Are the graves of dreams allowed to die."
-- Richard Harter


Op 2005-03-21,Robert Kern schreef< rk *** @ ucsd。 edu>:
Op 2005-03-21, Robert Kern schreef <rk***@ucsd.edu>:
Antoon Pardon写道:
Antoon Pardon wrote:
至少我发现它们不见了。

目前我经常遇到以下情况。

1)两个文件,每个文件都有同一个字典的键值对。
但是,如果第二个文件包含第一个不在第一个的键,则会出错在处理第二个文件时我错过了''set''方法。
dct.set(key,value)与dct [key] = value,如果密钥不在字典中,它会引发KeyError。

2)一个带键值对的文件。但是,如果文件中的密钥重复,则会出现错误。

在处理此类文件时,我会错过''make''方法。
dct.make(密钥,值)与dct [key] = value等效。
如果键已经在字典中,它会引发KeyError。

其他人怎么想关于这个?
Well at least I find them missing.

For the moment I frequently come across the following cases.

1) Two files, each with key-value pairs for the same dictionary.
However it is an error if the second file contains a key that
was not in the first file.

In treating the second file I miss a ''set'' method.
dct.set(key, value) would be equivallent to dct[key] = value,
except that it would raise a KeyError if the key wasn''t
already in the dictionary.
2) One file with key-value pairs. However it is an error
if a key is duplicated in the file.

In treating such files I miss a ''make'' method.
dct.make(key, value) would be equivallent to dct[key] = value.
except that it would raise a KeyError if the key was
already in the dictionary.
What do other people think about this?



如果key不在dct:
引发KeyError(键)
else:
dct [key] = value

def make(dct,key,value):
如果键入dct:
引发KeyError(''%r已经在dict''%key)
否则:
dct [key] = value

我没有看到一个很好的理由让这些内置到dict类型。



def safeset(dct, key, value):
if key not in dct:
raise KeyError(key)
else:
dct[key] = value

def make(dct, key, value):
if key in dct:
raise KeyError(''%r already in dict'' % key)
else:
dct[key] = value

I don''t see a good reason to make these built in to dict type.




我会说我们得到的原因相同。没有

的理由有一个内置得到它很容易实现

像这样:


def get(dct,key ,默认):


试试:

返回dct [key]

除了KeyError:

返回默认值

即使到目前为止我还有更多的理由要有内置的

safeset和make,而不是有理由内置得到。


原因是一个python实现的safeset和make,

将意味着在字典中进行两次访问,一次用于测试和

一次为作业。这种双重访问可以通过内置消除

。 get on另一方面只有一个字典

访问,所以在python中实现它是一个较小的负担。


-

Antoon Pardon



I would say the same reason that we have get. There is no
reason to have a builtin get it is easily implemented
like this:

def get(dct, key, default):

try:
return dct[key]
except KeyError:
return default
I would go even so far that there is more reason to have a built-in
safeset and make, than there is a reason to have a built-in get.

The reason is that a python implementation of safeset and make,
will mean two accesses in the dictionary, once for the test and
once for the assignment. This double access could be eliminated
with a built-in. The get on the other hand does only one dictionary
access, so having it implemeted in python is a lesser burden.

--
Antoon Pardon


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

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