list question

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • python101
    New Member
    • Sep 2007
    • 90

    list question

    I would like to write a function test(data) to test whether the list has two or more elements refer to the same underlying object
    [code=python]
    >>>a = range(2)
    >>>b = range(4)
    >>>c = range(2)
    >>>test([a,b,c])
    False
    >>>test([a,b,c,b]) #b appears 2 times
    True
    [/code]
    If it is a list of numbers or a list of strings like
    [1,2,3,4]
    ['A','B']
    I can write it but if it is a list contains many other lists I can't
  • bvdet
    Recognized Expert Specialist
    • Oct 2006
    • 2851

    #2
    Originally posted by python101
    I would like to write a function test(data) to test whether the list has two or more elements refer to the same underlying object
    [code=python]
    >>>a = range(2)
    >>>b = range(4)
    >>>c = range(2)
    >>>test([a,b,c])
    False
    >>>test([a,b,c,b]) #b appears 2 times
    True
    [/code]
    If it is a list of numbers or a list of strings like
    [1,2,3,4]
    ['A','B']
    I can write it but if it is a list contains many other lists I can't
    There is a difference between 'referring to the same underlying object' and two objects being equal.[code=Python]>>> a = range(4)
    >>> b = range(4)
    >>> a is b
    False
    >>> a == b
    True
    >>> b = a
    >>> a is b
    True
    >>> [/code]A list is an object like any other, and you can compare for equivalence. Are you wanting to check the lists inside your list recursively?

    Comment

    Working...