开发者

Checking if for-loop variable resides at two indexes of a list

This may be difficult to explain. I want to specify two indexes, and then run a for loop. If the current x 开发者_JAVA技巧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.


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.


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.


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 ... ?


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?

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜