如何让 Python 区分相等的整数

发布于 2024-12-21 12:05:13 字数 529 浏览 2 评论 0原文

在区分相同整数时遇到一些问题。

在下面(这显然是一个简单的情况)中,a、b、c 是整数。我希望创建一个字典,diction,它将包含 {a: 'foo', b: 'bar', c: 'baz'}

diction = {} 
for i in (a, b, c):
    j = ('foo', 'bar', 'baz')[(a, b, c).index(i)]
    diction[i] = j

所有运行都非常好,直到,例如,a 和 b 相同:第三行将为 a 和 b 提供索引 0,从而导致每种情况下 j = 'foo' 。

复制

list_a = [1, 2, 3]
list_b = list(list_a)

我知道列表可以通过or

list_b = list_a[:]

那么,有没有什么方法可以用我的相同整数来做到这一点?

(我尝试将其设为浮点数,但值保持不变,因此不起作用。)

Have a bit of a problem distinguishing between identical integers.

In the following (which is obviously a trivial case) a, b, c are integers. I wish to create a dicionary, diction, which will contain {a: 'foo', b: 'bar', c: 'baz'}

diction = {} 
for i in (a, b, c):
    j = ('foo', 'bar', 'baz')[(a, b, c).index(i)]
    diction[i] = j

All runs very nicely until, for example, a and b are the same: the third line will give index 0 for both a and b, resulting in j = 'foo' for each case.

I know lists can be copied by

list_a = [1, 2, 3]
list_b = list(list_a)

or

list_b = list_a[:]

So, is there any way of maybe doing this with my identical integers?

(I tried making one a float, but the value remains the same , so that doesn't work.)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

若言繁花未落 2024-12-28 12:05:13

要从两个不同的可迭代对象创建字典,可以使用以下代码:

d = dict(zip((a, b, c), ('foo', 'bar', 'baz')))

其中 zip 用于将两个可迭代对象组合在可以传递给字典构造函数的元组列表中。

请注意,如果 a==b,则 'foo' 将被 'bar' 覆盖,因为值已添加到字典中它们在可迭代中的顺序与您使用此代码时的顺序相同:

d[a] = 'foo'
d[b] = 'bar'
d[c] = 'baz'

这只是字典的标准行为,当将新值分配给已知的键时,该值将被覆盖。

如果您希望将所有值保留在列表中,则可以使用collections.defaultdict,如下所示:

from collections import defaultdict

d = defaultdict(list)
for key, value in zip((a, b, c), ('foo', 'bar', 'baz')):
    d[key].append(value)

To create a dictionary from two different iterables, you can use the following code:

d = dict(zip((a, b, c), ('foo', 'bar', 'baz')))

where zip is used to combine both iterables in a list of tuples that can be passed to the dictionary constructor.

Note that if a==b, then the 'foo' will be overwritten with 'bar', since the values are added to the dictionary in the same order they are in the iterable as if you were using this code:

d[a] = 'foo'
d[b] = 'bar'
d[c] = 'baz'

This is just the standard behaviour of a dictionary, when a new value is assigned to a key that is already known, the value is overwritten.

If you prefer to keep all values in a list, then you can use a collections.defaultdict as follows:

from collections import defaultdict

d = defaultdict(list)
for key, value in zip((a, b, c), ('foo', 'bar', 'baz')):
    d[key].append(value)
糖果控 2024-12-28 12:05:13

你无法区分相同的物体。

You can't distinguish between identical objects.

翻身的咸鱼 2024-12-28 12:05:13

如果它们不在 -5 和 256 之间,您可以区分它们

另请参阅 " is" 运算符对整数的行为异常

http://docs.python.org/c-api/int.html

当前的实现为所有对象保留一个整数对象数组
-5 到 256 之间的整数,当您创建该范围内的 int 时
实际上只是返回对现有对象的引用。所以它
应该可以改变 1 的值。我怀疑这种行为
在这种情况下,Python 的值是未定义的。 :-)

In [30]: a = 257

In [31]: a is 257
Out[31]: False

In [32]: a = 256

In [33]: a is 256
Out[33]: True

你可能必须滚动你自己的字典之类的对象来实现这种行为,但它仍然无法在 -5 到 256 之间做任何事情。我需要做更多的挖掘才能成为当然可以。

You can tell them apart if they do not fall between -5 and 256

See also "is" operator behaves unexpectedly with integers

http://docs.python.org/c-api/int.html

The current implementation keeps an array of integer objects for all
integers between -5 and 256, when you create an int in that range you
actually just get back a reference to the existing object. So it
should be possible to change the value of 1. I suspect the behaviour
of Python in this case is undefined. :-)

In [30]: a = 257

In [31]: a is 257
Out[31]: False

In [32]: a = 256

In [33]: a is 256
Out[33]: True

You may have to roll your own dictionary like object that implements this sort of behavior though... and it still wouldn't be able to do anything between -5 and 256. I'd need to do more digging to be sure though.

热鲨 2024-12-28 12:05:13

如果 a 和 b 具有相同的值,那么如果用作键,您不能期望它们指向字典中的不同位置。字典中的键值必须是唯一的。

另外,如果你有两个序列,用它们制作字典的最简单方法是将它们压缩在一起:

tup = (a,b,c)
val = ('foo', 'bar', 'baz')
diction = dict(zip(tup, val))

If a and b have the same value then you can't expect them to point to different positions in dictionary if used as keys. Key values in dictionaries must be unique.

Also if you have two sequences the simplest way to make a dictionary out of them is to zip them together:

tup = (a,b,c)
val = ('foo', 'bar', 'baz')
diction = dict(zip(tup, val))
把人绕傻吧 2024-12-28 12:05:13

到目前为止,所有答案都是正确的 - 相同的键不能在字典中重复使用。如果您绝对必须尝试做这样的事情,但无法确保a、b和c具有不同的值,您可以尝试这样的事情:

d = dict(zip((id(k) for k in (a,b,c)), ('foo', 'bar', 'baz')))

当您不过,如果要查找您的价值观,您必须记住这样做:

d[id(a)]

可能有帮助,但我不确定您实际上在这里追求什么。

All of the answers so far are correct - identical keys can't be re-used in a dictionary. If you absolutely have to try to do something like this, but can't ensure that a, b, and c have distinct values you could try something like this:

d = dict(zip((id(k) for k in (a,b,c)), ('foo', 'bar', 'baz')))

When you go to look up your values though, you'll have to remember to do so like this:

d[id(a)]

That might help, but I am not certain what you're actually after here.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文