By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,621 Members | 1,101 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,621 IT Pros & Developers. It's quick & easy.

list question

P: 90
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
Expand|Select|Wrap|Line Numbers
  1. >>>a = range(2)
  2. >>>b = range(4)
  3. >>>c = range(2)
  4. >>>test([a,b,c])
  5. False
  6. >>>test([a,b,c,b]) #b appears 2 times
  7. True
  8.  
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
Dec 5 '07 #1
Share this Question
Share on Google+
1 Reply


bvdet
Expert Mod 2.5K+
P: 2,851
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
Expand|Select|Wrap|Line Numbers
  1. >>>a = range(2)
  2. >>>b = range(4)
  3. >>>c = range(2)
  4. >>>test([a,b,c])
  5. False
  6. >>>test([a,b,c,b]) #b appears 2 times
  7. True
  8.  
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.
Expand|Select|Wrap|Line Numbers
  1. >>> a = range(4)
  2. >>> b = range(4)
  3. >>> a is b
  4. False
  5. >>> a == b
  6. True
  7. >>> b = a
  8. >>> a is b
  9. True
  10. >>> 
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?
Dec 5 '07 #2

Post your reply

Sign in to post your reply or Sign up for a free account.