了解使用d [key] = None [英] Understanding the use of d[key] = None

查看:94
本文介绍了了解使用d [key] = None的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

通过阅读一些代码学习一些python,并尝试了解在Dictionary上使用None。请你再点亮下面的代码片段。并且csv_dict中的pc的使用= =。

 
如果pc在shp_dict中:
匹配[pc] =无
else:
csv_not_in_shp [pc] =无

感谢

解决方案

我认为你是对的,被困惑,因为这并没有使我成为Python最好的例子编码。只需查看标识符名称,我们可以看到作者的意图:

 在csv_dict中的pc:
if pc在shp_dict中:
匹配[pc] =无
其他:
csv_not_in_shp [pc] =无

所以这里我们有一个 csv_dict shp_dict 写出两个名为匹配的另外两个数字 csv_not_in_shp 。很明显,以后的设置是类似的测试,作者可以看到:

 如果匹配的东西:
#在csv_dict和shp_dict之前的
elif东西在csv_not_in_shp:
#不是这样做别的

当您按照这种方式进行拆分时,您可以开始看到问题。例如,为什么我们需要 elif ?这可以很容易地被重写:

 如果匹配的东西:
#它在csv_dict和shp_dict之前
else:
#没有别的东西

这(可能是正确的)表示 csv_not_in_shp 根本不是必需的。



但是让我们给原作者怀疑的好处。可能 csv_dict 和 shp_dict 在此比较时不会出现。 (相当薄弱的原因,因为他们可能已经从大概相同的地方匹配 csv_not_in_shp 被传递,但... )这里的真实问题是作者使用作为该值,因为他不在乎该值是什么< STRONG>。换句话说,他非常使用它作为一个 NULL 占位符。



问题是,这是因为他没有将键映射到值。作者忽略了的明确用例。观察:

  matched = set()
在csv_dict中的pc:
如果pc在shp_dict中:
matched.add(pc)

现在我们说的是我们真正的意思。这是一个说明这个概念的基本示例。让 d 将字母a映射到数字0到4,而 d _ 对于第3到6号,我将使用d到g的字符映射(我将使用 OrderedDicts ,所以键以更可读的格式呈现,它们是否则不必要。)

 >>>导入字符串
>>>从集合导入OrderedDict as od
>>>> d = od([(k,v)for k,v in zip(string.ascii_lowercase [:5],range(5))])
>>> d_ = od([(k,v)for k,v in zip(string.ascii_lowercase [3:7],range(3,7))])
>>> d
OrderedDict([('a',0),('b',1),('c',2),('d',3),('e',4)] b $ b>>>> d_
OrderedDict([('d',3),('e',4),('f',5),('g',6)])
>> ; matched = set()
>>>关键d:
...如果键入d_:
... matched.add(key)
...
>>>匹配
{'e','d'}

有了一套,我们仍然可以在匹配中执行,这首先是我们想要的:

 >>> 'a'in matched 
False
>>> 'd'in matched
True

最好的事情当然是整个事情可以减少到一定的理解:

 >>> {key for d,如果键入d_} 
{'e','d'}

所以 matched = {pc for csv_dict如果pc在shp_dict} 将更正确地替换您的原始代码与一组,按照我的例子。


Learning some python by reading some code and trying to understand the usage of None on the dictionary's. Could you please bring some more light on the following snippet. and the usage of the = None .

for pc in csv_dict:
        if pc in shp_dict:
            matched[pc] = None
        else:
            csv_not_in_shp[pc] = None

Thanks

解决方案

I think you're right to be confused, because this doesn't strike me as the finest example of Python coding. Just looking at the identifier names, we can sort of see the author's intent:

for pc in csv_dict:
    if pc in shp_dict:
        matched[pc] = None
    else:
        csv_not_in_shp[pc] = None

So here we have a csv_dict and shp_dict coming in, and we're writing out to two more dicts called matched or csv_not_in_shp. Clearly what this is setting up for later is a similar kind of test where the author can see:

if thing in matched:
    # it was in both csv_dict and shp_dict earlier
elif thing in csv_not_in_shp:
    # it wasn't so do something else

When you break it down like this, you can already start to see the problems. For instance, why do we need elif here? That could easily be rewritten:

if thing in matched:
    # it was in both csv_dict and shp_dict earlier
else:
    # it wasn't so do something else

This (probably correctly) suggests that csv_not_in_shp isn't necessary at all.

But let's give the original author the benefit of the doubt. Maybe csv_dict and shp_dict won't be around at the time of this comparison. (Pretty weak reason since they could have been passed from presumably the same place matched and csv_not_in_shp were, but...) The real problem here is that the author is using None for the value because he doesn't care what the value is. In other words, he's very much using it as a NULL placeholder.

The problem is, this is because he's not mapping a key to a value. The author is overlooking a clear use case for sets. Observe:

matched = set()
for pc in csv_dict:
    if pc in shp_dict:
        matched.add(pc)

Now we are saying what we really mean. Here's a basic example illustrating the concept. Let d be a dict mapping the letters 'a' to 'e' to the numbers 0 through 4, and d_ be a dict mapping 'd' to 'g' to the numbers 3 through 6. (I will use OrderedDicts below so the keys are presented in a more readable format. They are otherwise unnecessary.)

>>> import string
>>> from collections import OrderedDict as od
>>> d = od([(k,v) for k, v in zip(string.ascii_lowercase[:5], range(5))])
>>> d_ = od([(k,v) for k, v in zip(string.ascii_lowercase[3:7], range(3,7))])
>>> d
OrderedDict([('a', 0), ('b', 1), ('c', 2), ('d', 3), ('e', 4)])
>>> d_
OrderedDict([('d', 3), ('e', 4), ('f', 5), ('g', 6)])
>>> matched = set()
>>> for key in d:
...   if key in d_:
...     matched.add(key)
... 
>>> matched
{'e', 'd'}

With a set, we can still do in matching, which is all we wanted in the first place:

>>> 'a' in matched
False
>>> 'd' in matched
True 

And the best thing, of course, is that the whole thing can be reduced down to a set comprehension:

>>> {key for key in d if key in d_}
{'e', 'd'}

So matched = {pc for pc in csv_dict if pc in shp_dict} would more correctly replace your original code with a set, following my example.

这篇关于了解使用d [key] = None的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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