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

How to get element value of array even if array is randomized

P: 1
Could anyone please suggest on how to fetch original data position of any element from an array even if the array gets randomized/shuffled.

e.g. my original array is: arr=[a,b,c,d,e]
and, arr[2] will return: [c]
and, after random.shuffle(arr) the output is=[c,a,e,d,b]
and, arr[2] will return: [e]

But, I still wanted to see the same result as from original array i.e. I should still get arr[2]=[c] even if I have randomized my array.

Please help in solving this query, thanks for your help on this!

Regards,
Akhilesh Gupta
Feb 20 '12 #1
Share this Question
Share on Google+
1 Reply

bvdet
Expert Mod 2.5K+
P: 2,851
You will have to save the original state of the list in some manner. This is sort of convoluted but seems to work:
Expand|Select|Wrap|Line Numbers
  1. import random
  2.  
  3. arr=['0', '1', '2', '3', '4', '5']
  4.  
  5. dd = {}
  6. for i, item in enumerate(arr):
  7.     dd[i] = item 
  8. random.shuffle(arr)
  9. print arr
  10.  
  11. i = arr.index(dd[2])
  12.  
  13. print arr[i]
  14.  
Feb 20 '12 #2

Post your reply

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