检查 for 循环变量是否驻留在列表的两个索引处
这可能很难解释。我想指定两个索引,然后运行 for 循环。如果循环中当前检查的x
与两个索引位置指定的完全相同,那么它应该返回一条消息。
array = [ [1,1,1,1], [2,2,2,2], [3,3,3,3], [4,4,4,4] ]
再说一遍,这是我的数组。我希望它通过 for 循环并在 x
是 array[0][0]
内存中的 1
时打印消息,又名同一段内存。
for x in array:
if x == array[0][0]:
print "%s is the object you're looking for." % x
现在,为什么我需要它来确保它是内存中相同的对象,因为这将循环第一个列表中的以下三个 1,并返回消息,因为它们具有相同的值 em> 作为第一个 1。我不需要需要这个。我只需要匹配内存中的实际点,而不是值。
This may be difficult to explain. I want to specify two indexes, and then run a for loop. If the current x
being checked in the loop is the exact same item specified at the location of the two indexes, then it should return a message.
array = [ [1,1,1,1], [2,2,2,2], [3,3,3,3], [4,4,4,4] ]
Again, here's my array. I want it to go through the for loop and print the message out when x
is the 1
at memory of array[0][0]
, aka the same piece of memory.
for x in array:
if x == array[0][0]:
print "%s is the object you're looking for." % x
Now, why I need it to make sure it is the same exact object in memory is because, this would loop through the following three 1's in the first list, and return the message too, as they have the same value as the first 1. I do not need this. I need only to match actual points in memory, not values.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我认为您要问的是如何测试对象身份而不是对象平等。
在 Python 中,您可以使用
is
运算符 来完成此操作,即而不是:
但是,您将遇到整数和字符串问题,因为Python运行时会自动重用它从池中为这些对象创建的对象:
我认为短字符串会以这种方式自动“驻留”,您可以强制使用
intern()
函数 较长的字符串:但是,似乎
long
类型没有被保留,因此您可以使用它:但是,内置类型的对象标识取决于 Python 的实现而不是语言中的实现,并且不是无论哪种方式你都应该依赖的东西。我认为你最好创建自己的对象类并为它们编写适当的 __eq__ 方法,即编写基于对象相等而不是身份的代码。
I think what you're asking is how to test object identity rather than object equality.
In Python you can do this with the
is
operator, i.e.Rather than:
However, you're going have problems with ints and strings because the Python runtime automatically re-uses objects it creates for these from a pool:
I think short strings are automatically "interned" in this way and you can force longer strings to be using the
intern()
function:However, it seems the
long
type isn't interned so you use that:However, object identity for built-in types is down to the implementation of Python rather than being in the language, and is not something you should rely on either way. I think you would be better off creating your own class of objects and writing an appropriate
__eq__
method for them, i.e. write your code it works based on object equality rather than identity.实际上 x 它不是一个内部元组元素,而是一个完整的内部元组。
如果我很好地理解你,你想打印整个元组,其中
index
元素与array[container][index]
相同,如下所示:导致
[ 1, 1, 1, 1] 是您要查找的对象。
Actually x it's not a inner-tuple element, but a whole inner-tuple.
If I understood you well you want to print the whole tuple where the
index
element is the same asarray[container][index]
like this:Resulting in
[1, 1, 1, 1] is the object you're looking for.
我不明白你想要做什么:
for x in array:
意味着x
首先是[1,1,1,1]
然后[2,2,2,2]
等等。另一方面,
array[container][index]
只能是1
...它们怎么可能相等呢?无论如何,将
is
与数字一起使用一开始就没有意义。也许你应该问一下真正的问题......?I don't get what you're trying to do:
for x in array:
means thatx
will first be[1,1,1,1]
then[2,2,2,2]
and so on.On the other hand,
array[container][index]
can be only1
... how could they ever be equal?Anyways, using
is
with numbers makes no sense to begin with. Maybe you should ask about the real problem ... ?a) 这些是列表,不是元组,也不是数组
b)
for x in array:
遍历列表“array”,例如 x 是 [1,1,1,1],然后是 [2,2,2,2],...
而且这些不在 array[container][index] 的位置是很清楚的,不是吗? (挑剔的是,它们可能在那里,因为列表可以包含自身,但我不认为这就是你想要的)。
你到底想做什么?
a) these are Lists, not tuples and not arrays
b)
for x in array:
goes through the list "array", e.g. x is [1,1,1,1], then [2,2,2,2], ...
and that these are not at the position array[container][index] is clear, isn't it? (to be nitpicking, it would be possible for them to be there, because a list can contain itself, but I don't think that is what you want).
What do you want to do exactly?