检查 for 循环变量是否驻留在列表的两个索引处

发布于 2024-10-20 22:47:08 字数 547 浏览 6 评论 0原文

这可能很难解释。我想指定两个索引,然后运行 ​​for 循环。如果循环中当前检查的x 与两个索引位置指定的完全相同,那么它应该返回一条消息。

array = [ [1,1,1,1], [2,2,2,2], [3,3,3,3], [4,4,4,4] ]

再说一遍,这是我的数组。我希望它通过 for 循环并在 xarray[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 技术交流群。

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

发布评论

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

评论(4

半寸时光 2024-10-27 22:47:08

我认为您要问的是如何测试对象身份而不是对象平等

在 Python 中,您可以使用 is 运算符 来完成此操作,即

if x is y:

而不是:

if x == y:

但是,您将遇到整数和字符串问题,因为Python运行时会自动重用它从池中为这些对象创建的对象:

>>> a = 1
>>> b = 1
>>> a is b
True
>>> id(a),id(b)
(13561552, 13561552)

我认为短字符串会以这种方式自动“驻留”,您可以强制使用 intern() 函数 较长的字符串:

>>> c = 'Long string'
>>> d = 'Long string'
>>> c is d
False
>>> c = intern(c)
>>> d = intern(d)
>>> c is d
True

但是,似乎 long 类型没有被保留,因此您可以使用它:

>>> a = 1L
>>> b = 1L
>>> a is b
False

但是,内置类型的对象标识取决于 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.

if x is y:

Rather than:

if x == y:

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:

>>> a = 1
>>> b = 1
>>> a is b
True
>>> id(a),id(b)
(13561552, 13561552)

I think short strings are automatically "interned" in this way and you can force longer strings to be using the intern() function:

>>> c = 'Long string'
>>> d = 'Long string'
>>> c is d
False
>>> c = intern(c)
>>> d = intern(d)
>>> c is d
True

However, it seems the long type isn't interned so you use that:

>>> a = 1L
>>> b = 1L
>>> a is b
False

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.

阳光下的泡沫是彩色的 2024-10-27 22:47:08

实际上 x 它不是一个内部元组元素,而是一个完整的内部元组。

如果我很好地理解你,你想打印整个元组,其中 index 元素与 array[container][index] 相同,如下所示:

for subtuple in array:
    if subtuple[index] is array[container][index]:
        print "%s is the object you're looking for." % subtuple

导致 [ 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 as array[container][index] like this:

for subtuple in array:
    if subtuple[index] is array[container][index]:
        print "%s is the object you're looking for." % subtuple

Resulting in [1, 1, 1, 1] is the object you're looking for.

幽梦紫曦~ 2024-10-27 22:47:08

我不明白你想要做什么:

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 that x will first be [1,1,1,1] then [2,2,2,2] and so on.
On the other hand, array[container][index] can be only 1 ... 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 ... ?

淡淡離愁欲言轉身 2024-10-27 22:47:08

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?

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