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,
You may just shuffle the list in place instead of creating a new list and a new number every time:
Column_Numbers = list(np.arange(1,TotalNumbcol+1,1))
random.shuffle(Column_Numbers)
while Column_Numbers:
rand = Column_Numbers.pop()
print(rand)
I've got this piece of coding which, when displayed in Firefox or Chrome, throws an exception (the image cannot be displayed because it contains errors).
byte[] img2 = { 105, 86, 66, 79, 82, 119, 48, 75, 71, 103, 111, 65, 65, 65, 65, 78, 83, 85, 104, 69, 85, 103,
65, 65, 65, 68, 65, 65, 65, 65, 65, 101, 67, 65, 89, 65, 65, 65, 66, 113, 112, 74, 51, 66, 65, 65, 65,
65, 71, 88, 82, 70, 87, 72, 82, 84, 98, 50, 90, 48, 100, 50, 70, 121, 90, 81, 66, 66, 90, 71, 57, 105,
90, 83, 66, 74, 98, 87, 70, 110, 90, 86, 74, 108, 89, 87, 82, 53, 99, 99, 108, 108, 80, 65, 65, 65, 65,
50, 112, 74, 82, 69, 70, 85, 101, 78, 114, 115, 87, 69, 49, 73, 86, 71, 69, 85, 118, 102, 54, 107, 111,
87, 108, 69, 97, 101, 85, 89, 113, 73, 115, 89, 85, 55, 82, 78, 106, 107, 81, 104, 87, 81, 109, 90,
103, 90, 67, 103, 76, 105, 113, 85, 119, 69, 88, 89, 72, 119, 81, 117, 99, 117, 85, 109, 107, 66, 90,
87, 71, 122, 99, 87, 69, 112, 82, 85, 71, 66, 81, 73, 47, 85, 99, 82, 97, 89, 69, 74, 97, 111, 71, 107,
107, 87, 78, 81, 75, 108, 79, 97, 83, 85, 111, 47, 99, 57, 53, 52, 72, 47, 102, 55, 51, 110, 115, 122,
85, 113, 115, 72, 99, 43, 69, 78, 56, 55, 53, 53, 99, 57, 56, 57, 53, 53, 53, 55, 118, 106, 99, 84,
107, 49, 86, 53, 113, 88, 57, 76, 57, 114, 113, 116, 113, 99, 109, 74, 53, 75, 97, 89, 109, 102, 116,
74, 119, 50, 78, 84, 98, 54, 106, 56, 49, 80, 85, 47, 98, 103, 51, 85, 72, 117, 115, 50, 53, 109, 87,
103, 57, 108, 104, 121, 101, 85, 81, 66, 82, 65, 72, 56, 90, 56, 84, 106, 53, 102, 102, 67, 78, 49,
111, 77, 68, 78, 107, 106, 84, 69, 105, 108, 70, 87, 118, 121, 76, 101, 116, 66, 67, 122, 79, 115, 106,
67, 78, 111, 120, 101, 84, 47, 77, 109, 116, 90, 107, 121, 97, 66, 122, 43, 55, 49, 106, 116, 72, 69,
53, 75, 121, 53, 53, 107, 108, 76, 111, 84, 74, 102, 78, 117, 108, 109, 115, 109, 83, 84, 121, 115, 65,
105, 110, 121, 48, 65, 120, 75, 43, 53, 99, 90, 111, 102, 55, 54, 70, 53, 102, 52, 47, 108, 111, 114,
81, 57, 51, 90, 83, 52, 102, 114, 117, 83, 102, 80, 47, 112, 76, 105, 88, 53, 115, 47, 90, 68, 49, 78,
76, 120, 110, 70, 52, 79, 84, 112, 106, 114, 65, 49, 101, 80, 109, 116, 101, 51, 100, 98, 50, 105, 106,
106, 115, 68, 116, 105, 84, 100, 101, 117, 121, 104, 97, 121, 50, 86, 121, 108, 112, 116, 56, 50, 48,
70, 65, 71, 75, 115, 43, 53, 105, 57, 104, 71, 73, 84, 86, 108, 78, 83, 84, 106, 87, 116, 76, 98, 108,
67, 71, 121, 116, 102, 85, 50, 66, 86, 117, 88, 75, 82, 68, 117, 114, 109, 111, 51, 102, 75, 79, 84,
79, 111, 77, 56, 97, 115, 78, 112, 122, 114, 99, 83, 119, 101, 85, 90, 122, 118, 85, 99, 55, 82, 74,
98, 49, 52, 55, 110, 114, 69, 71, 89, 104, 76, 51, 107, 82, 120, 66, 97, 49, 48, 47, 79, 107, 82, 43,
114, 54, 52, 48, 108, 104, 98, 68, 65, 120, 113, 106, 75, 107, 65, 84, 108, 81, 88, 109, 85, 120, 76,
43, 84, 66, 89, 50, 82, 87, 65, 113, 106, 57, 81, 83, 67, 100, 114, 105, 113, 105, 113, 49, 71, 117,
99, 81, 48, 89, 121, 76, 116, 43, 49, 66, 50, 115, 72, 73, 78, 55, 117, 81, 116, 122, 56, 47, 89, 47,
78, 81, 82, 67, 72, 54, 85, 74, 74, 74, 57, 72, 110, 70, 48, 111, 83, 109, 81, 106, 115, 90, 97, 97,
110, 75, 69, 86, 75, 65, 70, 76, 118, 105, 79, 98, 54, 72, 85, 98, 104, 72, 75, 50, 78, 75, 110, 68,
77, 107, 90, 54, 76, 111, 51, 100, 111, 81, 118, 108, 117, 87, 66, 99, 113, 122, 115, 43, 103, 107, 97,
56, 98, 68, 66, 68, 111, 66, 72, 100, 66, 90, 43, 102, 103, 76, 113, 57, 53, 89, 120, 107, 54, 113,
120, 121, 89, 69, 49, 50, 67, 99, 111, 68, 98, 117, 118, 113, 85, 100, 83, 109, 118, 90, 85, 109, 73,
119, 53, 99, 88, 43, 105, 74, 65, 100, 65, 121, 88, 109, 67, 52, 70, 102, 88, 75, 65, 101, 87, 90, 69,
90, 53, 111, 55, 52, 77, 118, 76, 115, 76, 106, 76, 109, 89, 115, 80, 97, 87, 100, 68, 112, 52, 86,
112, 100, 105, 107, 53, 87, 47, 56, 77, 111, 77, 121, 88, 89, 55, 54, 47, 77, 101, 75, 106, 106, 51,
54, 47, 119, 90, 120, 115, 100, 49, 86, 112, 114, 118, 110, 101, 83, 85, 73, 111, 111, 76, 86, 120,
116, 121, 85, 47, 79, 103, 97, 110, 107, 100, 51, 81, 56, 57, 100, 86, 70, 70, 74, 117, 108, 109, 113,
100, 43, 110, 48, 99, 65, 89, 66, 100, 72, 66, 122, 110, 110, 51, 106, 111, 102, 116, 43, 111, 114, 88,
120, 67, 66, 99, 51, 89, 79, 108, 65, 73, 113, 78, 101, 119, 83, 100, 49, 116, 87, 70, 74, 99, 116, 74,
81, 110, 55, 103, 48, 83, 73, 71, 86, 49, 68, 106, 53, 70, 72, 109, 73, 53, 111, 77, 122, 81, 103, 47,
55, 112, 52, 79, 117, 48, 85, 112, 81, 69, 75, 71, 102, 65, 98, 115, 78, 66, 76, 104, 121, 81, 67, 67,
84, 69, 82, 102, 79, 71, 78, 84, 79, 51, 89, 74, 107, 106, 100, 77, 106, 113, 82, 74, 80, 76, 102, 53,
84, 89, 87, 53, 84, 106, 43, 74, 108, 107, 51, 48, 107, 43, 87, 74, 101, 83, 89, 70, 49, 76, 52, 69,
55, 87, 121, 87, 54, 107, 53, 57, 98, 110, 73, 69, 73, 72, 77, 104, 122, 108, 74, 101, 87, 103, 77, 56,
102, 121, 119, 101, 54, 76, 65, 112, 106, 57, 107, 65, 109, 77, 87, 106, 97, 56, 122, 80, 82, 85, 82,
43, 117, 48, 109, 120, 48, 99, 84, 69, 74, 56, 112, 66, 56, 77, 89, 70, 78, 72, 122, 82, 115, 88, 104,
43, 53, 65, 55, 68, 122, 56, 80, 84, 115, 109, 53, 98, 52, 103, 114, 82, 79, 66, 84, 85, 54, 54, 70,
47, 74, 103, 86, 117, 81, 53, 65, 52, 106, 52, 78, 75, 111, 80, 72, 107, 66, 66, 66, 117, 71, 99, 65,
89, 122, 113, 79, 55, 78, 100, 70, 57, 117, 98, 121, 111, 49, 99, 48, 106, 113, 82, 118, 54, 54, 105,
119, 79, 119, 97, 68, 117, 122, 99, 77, 116, 53, 43, 109, 70, 113, 101, 104, 69, 74, 115, 98, 119, 118,
79, 81, 114, 97, 106, 119, 122, 67, 76, 115, 108, 104, 109, 66, 119, 57, 122, 89, 69, 115, 54, 82, 48,
112, 83, 119, 112, 75, 55, 101, 69, 121, 81, 55, 85, 51, 55, 119, 117, 90, 72, 121, 73, 99, 57, 53,
102, 79, 97, 115, 57, 50, 117, 47, 86, 71, 80, 50, 113, 79, 47, 121, 75, 73, 65, 111, 103, 68, 99, 68,
105, 67, 99, 86, 55, 115, 104, 89, 116, 122, 43, 53, 43, 53, 102, 65, 81, 89, 65, 80, 115, 81, 106,
121, 52, 54, 108, 99, 104, 65, 65, 65, 65, 65, 65, 83, 85, 86, 79, 82, 75, 53, 67, 89, 73, 73, 61 };
response.setContentType("image/jpeg");
response.setContentLength(img2.length);
response.getOutputStream().write(img2, 0, img2.length);
response.getOutputStream().close();
If I embed the same piece in an HTML it all works okay.
Can anybody tell me why?
<img src="data:image/jpeg;base64,iVBORw0KGgoAAAANSUhEUgAAADAAAAAeCAYAAABqpJ3BAAAAGXRFWHRTb2Z0d2FyZQBBZG9iZSBJbWFnZVJlYWR5ccllPAAAA2pJREFUeNrsWE1IVGEUvf6koWlEaeUYqIsYU7RNjkQhWQmZgZCgLiqUwEXYHwQucuUmkBZWGzcWEpRUGBQI/UcRaYEJaoGkkWNQKlOaSUo/c954H/f73nszUqsHc+EN8755c9895557vjcTk1V5qX9L9rqtqcmJ5KaYmftJw2NTb6j81PU/bg3UHus25mWg9lhyeUQBRAH8Z8Tj5ffCN1oMDNkjTEilFWvyLetBCzOsjCNoxeT/MmtZkyaBz+71jtHE5Ky55klLoTJfNulmsmSTysAiny0AxK+5cZof76F5f4/lorQ93ZS4fruSfP/pLiX5s/ZD1NLxnF4OTpjrA1ePmte3db2ijjsDtiTdeuyhay2Vylpt820FAGKs+5i9hGITVlNSTjWtLblCGytfU2BVuXKRDurmo3fKOTOoM8asNpzrcSweUZzvUc7RJb147nrEGYhL3kRxBa10/OkR+r640lhbDAxqjKkATlQXmUxL+TBY2RWAqj9QSCdriqiq1GucQ0YyLt+1B2sHIN7uQtz8/Y/NQRCH6UJJJ9HnF0oSmQjsZaanKEVKAFLviOb6HUbhHK2NKnDMkZ6Lo3doQvluWBcqzs+gka8bDBDoBHdBZ+fgLq95Yxk6qxyYE12CcoDbuvqUdSmvZUmIw5cX+iJAdAyXmC4FfXKAeWZEZ5o74MvLsLjLmYsPaWdDp4Vpdik5W/8MoMyXY76/MeKjj36/wZxsd1VprvneSUIooLVxtyU/Ogankd3Q89dVFFJulmqd+n0cAYBdHBznn3joft+orXxCBc3YOlAIqNewSd1tWFJctJQn7g0SIGV1Dj5FHmI5oMzQg/7p4Ou0UpQEKGfAbsNBLhyQCCTERfOGNTO3YJkjdMjqRJPLf5TYW5Tj+Jlk30k+WJeSYF1L4E7WyW6k59bnIEIHMhzlJeWgM8fywe6LApj9kAmMWja8zPRUR+u0mx0cTEJ8pB8MYFNHzRsXh+5A7Dz8PTsm5b4grROBTU66F/JgVuQ5A4j4NKoPHkBBBuGcAYzqO7NdF9ubyo1c0jqRv66iwOwaDuzcMt5+mFqehEJsbwvOQrajwzCLslhmBw9zYEs6R0pSwpK7eEyQ7U37wuZHyIc95fOas92u/VGP2qO/yKIAogDcDiCcV7shYtz+5+5fAQYAPsQjy46lchAAAAAASUVORK5CYII="/>
If the byte array is a JPEG, it should start with a standard JPEG signature, but yours doesn't. It's hard to tell what's going on since you didn't provide any information about where these bytes came from, but what you should be looking for is something like this:
255 216 255 224 0 16 74 70 73 70
for the first 9 bytes -- the last 4 of those spell "JFIF"
This can vary, but the first two bytes should definitely be FFC0 - start of image
What's a mystery is why the base64-encoded image displays correctly.
Oh I see now: it's a PNG not a JPEG. Check this answer: PHP : binary image data, checking the image type it should help.
There also seems to be something wrong with your conversion since the first bytes of your array are not the same as the first bytes of the base64 png (those are: 0x89, 0x50, 0x4e, or in decimal: 137, 80, 78)