在同一个字典中为假和0 [英] False and 0 in the same dictionary

查看:85
本文介绍了在同一个字典中为假和0的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经使用Python一段时间(4年)所以我觉得自己像个白痴

写这篇文章是因为我觉得我应该知道这个答案

问题:


如何制作一个包含0个不同键值对的字典,

False,1和True。

据我所知,0和False都散列为相同的值(1

和True相同)。


>> b = {0:''xyz'',False:''abc''}
b



{0:''abc''}#我是唯一认为这很奇怪的人吗?


这显然源于0 ==错误但0不是假的事实

等等。


这对我的情况没有帮助我需要区分两个


同样的问题适用于清单:


假设我这样做:


>> a = [0,1,True,False]
a.index(假)



0


Wha ???帮助。

解决方案

Prateek< su ***** @ gmail.comwrote:


我已经使用Python一段时间了(4年)所以我觉得自己像个白痴

写这篇文章是因为我觉得我应该知道这个答案

问题:


如何制作一个具有0个不同键值对的字典,

False,1和True。



如何使用(x,type(x))作为键而不仅仅是x?


11月5日,1:52 * am,Duncan Booth< duncan.bo ... @ invalid.invalidwrote:


Prateek< sure。 .. @ gmail.comwrote:


我已经使用Python一段时间(4年)所以我觉得自己像个白痴

写这篇文章是因为我想我应该知道这个问题的答案

问题:


如何制作字典它有0个不同的键值对,

False,1和True。



如果使用(x,type(x))作为键而不仅仅是x?



是的。我想到了这一点。虽然看起来有点单调。

特别是因为字典基本上是一个缓存,主要包含

字符串。添加额外元组的所有内存开销似乎只是为了这四个键而浪费了。


还有更好的方法吗?

我还想过在自定义类中使用自定义的__eq__方法

扩展了dict类型但决定更糟糕。


Prateek


11月4日,4:21 * pm,Prateek< sure ... @ gmail.comwrote:


11月5日,1:52 * am,Duncan Booth< duncan.bo ... @ invalid.invalidwrote:


Prateek< sure ... @ gmail.comwrote:


我已经使用Python一段时间(4年)所以我觉得自己像个白痴

写作这篇文章因为我想我应该知道这个问题的答案

问题:


如何制作具有独特性的字典键值对0,

False,1和True。


如何使用(x,type(x))作为键而不仅仅是x?



是的。我想到了这一点。虽然看起来有点单调。

特别是因为字典基本上是一个缓存,主要包含

字符串。添加额外元组的所有内存开销似乎只是为了这四个键而浪费了。


还有更好的方法吗?

我还想过在自定义类中使用自定义的__eq__ *方法

扩展了dict类型,但决定更糟糕。


Prateek



嗯,我的原始回复没有出现。


我很好奇你的意思'试图完成。


请记住,我输入的回复不知道你的应用程序。

虽然Python不是静态类型的语言,0和错误是

本质上是不同的类型(int和bool)。将它们作为键存储

字典只是看起来不是一个好的设计。


I''ve been using Python for a while (4 years) so I feel like a moron
writing this post because I think I should know the answer to this
question:

How do I make a dictionary which has distinct key-value pairs for 0,
False, 1 and True.
As I have learnt, 0 and False both hash to the same value (same for 1
and True).

>>b = {0:''xyz'', False:''abc''}
b

{0: ''abc''} # Am I the only one who thinks this is weird?

This obviously stems from the fact that 0 == False but 0 is not False
etc. etc.

That doesn''t help my case where I need to distinguish between the two

The same issue applies in a list:

Suppose I do:

>>a = [0, 1, True, False]
a.index(False)

0

Wha??? Help.

解决方案

Prateek <su*****@gmail.comwrote:

I''ve been using Python for a while (4 years) so I feel like a moron
writing this post because I think I should know the answer to this
question:

How do I make a dictionary which has distinct key-value pairs for 0,
False, 1 and True.

How about using (x, type(x)) as the key instead of just x?


On Nov 5, 1:52*am, Duncan Booth <duncan.bo...@invalid.invalidwrote:

Prateek <sure...@gmail.comwrote:

I''ve been using Python for a while (4 years) so I feel like a moron
writing this post because I think I should know the answer to this
question:

How do I make a dictionary which has distinct key-value pairs for 0,
False, 1 and True.


How about using (x, type(x)) as the key instead of just x?

Yup. I thought of that. Although it seems kinda unpythonic to do so.
Especially since the dictionary is basically a cache mostly containing
strings. Adding all the memory overhead for the extra tuples seems
like a waste just for those four keys.

Is there a better way?
I also thought of using a custom __eq__ method in a custom class
which extends the dict type but decided that was even worse.

Prateek


On Nov 4, 4:21*pm, Prateek <sure...@gmail.comwrote:

On Nov 5, 1:52*am, Duncan Booth <duncan.bo...@invalid.invalidwrote:

Prateek <sure...@gmail.comwrote:

I''ve been using Python for a while (4 years) so I feel like a moron
writing this post because I think I should know the answer to this
question:

How do I make a dictionary which has distinct key-value pairs for 0,
False, 1 and True.

How about using (x, type(x)) as the key instead of just x?


Yup. I thought of that. Although it seems kinda unpythonic to do so.
Especially since the dictionary is basically a cache mostly containing
strings. Adding all the memory overhead for the extra tuples seems
like a waste just for those four keys.

Is there a better way?
I also thought of using a custom __eq__ *method in a custom class
which extends the dict type but decided that was even worse.

Prateek

Hmm, my original reply didn''t show up.

I''m curious as to what you''re trying to accomplish.

Bear in mind that I type this response not knowing your application.
While Python is not a statically typed language, 0 and False are
essentially different types (int and bool). Storing them both as keys
of a dictionary just doesn''t seem like a good design.


这篇关于在同一个字典中为假和0的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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