mercredi 22 juin 2016

Faster way for randomly chosing a value from list and deleting the chosen value

I was wondering if there is a faster way to randomly choose a value from a list and deleting this value from this list so it cannot be chosen again. This drawing of a value will continue until there aint no values left anymore.

The way I did it soved the problem but it takes almost 8 seconds. So I'm wondering if there is a faster way. I am using Jupyter notebook through the Anaconda software. Since this goes through a server, could it be the problem?

This is what I did:

TotalNumbcol = 266


 Column_Numbers = list(np.arange(1,TotalNumbcol+1,1)) # creating a list with all column numbers in it from which can be drawn.
#print Column_Numbers
ABC = Column_Numbers # Creating a variable for the len command in the for loop below, since the Column Numbers length will change.
Chosen_Columns = [[0] for i in range(0,len(Column_Numbers))]

for i in range(len(ABC)): 
    RandChoiceCol = int(random.choice(Column_Numbers)) # chosing a random number from the Column_Numbers range
    Chosen_Columns[i]=(RandChoiceCol) # adding each randomly chosen column number to a list in list showing which column has been chosen.
    Column_Numbers = [x for x in Column_Numbers if x not in Chosen_Columns] # delete chosen_column from RandChoiceCol

print Chosen_Columns

print Column_Numbers


[21, 131, 145, 218, 153, 60, 201, 15, 158, 189, 230, 210, 18, 103, 69, 76, 226, 180, 67, 187, 238, 20, 157, 24, 48, 11, 47, 117, 101, 51, 122, 155, 109, 225, 86, 243, 146, 30, 58, 7, 66, 132, 22, 110, 1, 142, 234, 245, 266, 129, 232, 39, 184, 49, 114, 182, 162, 144, 92, 126, 5, 254, 150, 102, 135, 173, 36, 52, 42, 26, 228, 63, 17, 8, 163, 40, 78, 174, 222, 205, 183, 140, 221, 70, 125, 72, 247, 237, 64, 246, 185, 130, 248, 90, 197, 53, 107, 77, 108, 256, 207, 139, 176, 192, 2, 164, 4, 124, 241, 113, 188, 178, 235, 265, 190, 212, 99, 175, 79, 231, 257, 202, 50, 242, 181, 46, 161, 133, 104, 28, 251, 213, 204, 59, 149, 252, 179, 43, 137, 195, 160, 220, 119, 74, 87, 255, 98, 208, 105, 239, 170, 203, 167, 136, 250, 134, 32, 165, 229, 9, 258, 13, 141, 240, 262, 34, 227, 148, 41, 111, 54, 71, 61, 94, 249, 29, 75, 10, 193, 152, 73, 123, 65, 6, 116, 68, 91, 56, 25, 233, 156, 261, 35, 171, 211, 215, 186, 154, 138, 200, 44, 112, 57, 166, 120, 147, 89, 31, 106, 118, 199, 198, 81, 223, 83, 12, 214, 45, 121, 244, 95, 168, 55, 37, 206, 263, 93, 196, 115, 169, 217, 236, 82, 143, 96, 33, 209, 14, 100, 216, 128, 259, 219, 151, 16, 177, 159, 23, 38, 84, 80, 27, 19, 264, 62, 85, 127, 97, 224, 172, 191, 88, 253, 3, 260, 194]
[]

If there is a more efficient way saving time please let me know.

Regards,




Aucun commentaire:

Enregistrer un commentaire