473,406 Members | 2,377 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

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
Feb 20 '12 #1
1 1596
bvdet
2,851 Expert Mod 2GB
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

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

Similar topics

12
by: JKop | last post by:
template<class T> inline T& NthArrayMember( T & (array),size_t i) { return array; } template<class T> inline const T& NthArrayMember( const T & (array),size_t i) { return array;
20
by: Geoff Cox | last post by:
Hello, This may be the answer to a problem I have - or it may not be possible?! Can I have initial values of variable situation and count defined as var situation = 0; var count = 0;
7
by: fdunne2 | last post by:
Hi, Is there a function in C that returns the max value in an array? I need a function that returns the max value and the corresponding array index. Also, when reading float values from a...
2
by: worli | last post by:
Hi All, I have a strange requirement. I have a dynamic input numeric data stream e.g. 2, 2, 4, 5 etc.... ( each input number range from 1 to 10 ). lets take a simple case where all inputs will...
2
by: Amrit Kohli | last post by:
Hello. I have the following code, to do a simple operation by copying the elements of a vector of strings into an array of char pointers. However, when I run this code, the first element in the...
2
by: avharut | last post by:
hello everyone! sorry to disturb you with this, but i really cant find anything suitable about the topic in the numarray documentation. how can i find in an efficient way the index of the last...
4
by: aqazi | last post by:
Hi guys I am having a problem with arrayu manipulation. in my php script i am reading from a csv file. the content of file is like this: name,color,quantity,price; apple,red,10,$2;...
8
by: =?big5?B?r0W84Q==?= | last post by:
Hi All C gurus: Below is a small program to print out the address of array and address of array variable: int main() { char array = "haha"; printf("array:%x\n", array); printf("&array:%x\n",...
7
by: =?utf-8?B?5YiY5piK?= | last post by:
Hi, folks, Is it possible to delete an element from a sorted array with O(1) time? Best regards
2
choke
by: choke | last post by:
I need to write a function in devc++ that creates an array of n integers, each element is equal to n*n+i*i-n*i where i is from 0 to n-1 as the array index. Within the same function I need to find...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.