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

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Akhilesh Gupta
    New Member
    • Feb 2012
    • 1

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

    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
    Last edited by bvdet; Feb 20 '12, 03:52 PM. Reason: Remove code tags (this is a first!).
  • bvdet
    Recognized Expert Specialist
    • Oct 2006
    • 2851

    #2
    You will have to save the original state of the list in some manner. This is sort of convoluted but seems to work:
    Code:
    import random
    
    arr=['0', '1', '2', '3', '4', '5']
    
    dd = {}
    for i, item in enumerate(arr):
        dd[i] = item 
    random.shuffle(arr)
    print arr
    
    i = arr.index(dd[2])
    
    print arr[i]

    Comment

    Working...