如何让 Python 区分相等的整数
在区分相同整数时遇到一些问题。
在下面(这显然是一个简单的情况)中,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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
要从两个不同的可迭代对象创建字典,可以使用以下代码:
其中
zip
用于将两个可迭代对象组合在可以传递给字典构造函数的元组列表中。请注意,如果
a==b
,则'foo'
将被'bar'
覆盖,因为值已添加到字典中它们在可迭代中的顺序与您使用此代码时的顺序相同:这只是字典的标准行为,当将新值分配给已知的键时,该值将被覆盖。
如果您希望将所有值保留在列表中,则可以使用
collections.defaultdict
,如下所示:To create a dictionary from two different iterables, you can use the following code:
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: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:你无法区分相同的物体。
You can't distinguish between identical objects.
如果它们不在 -5 和 256 之间,您可以区分它们
另请参阅 " is" 运算符对整数的行为异常
http://docs.python.org/c-api/int.html
你可能必须滚动你自己的字典之类的对象来实现这种行为,但它仍然无法在 -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
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.
如果 a 和 b 具有相同的值,那么如果用作键,您不能期望它们指向字典中的不同位置。字典中的键值必须是唯一的。
另外,如果你有两个序列,用它们制作字典的最简单方法是将它们压缩在一起:
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:
到目前为止,所有答案都是正确的 - 相同的键不能在字典中重复使用。如果您绝对必须尝试做这样的事情,但无法确保a、b和c具有不同的值,您可以尝试这样的事情:
当您不过,如果要查找您的价值观,您必须记住这样做:
可能有帮助,但我不确定您实际上在这里追求什么。
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:
When you go to look up your values though, you'll have to remember to do so like this:
That might help, but I am not certain what you're actually after here.