Related
This question already has answers here:
How to slice a pandas DataFrame by position?
(5 answers)
Closed 24 days ago.
I am trying to replace the following code:
DfInt['Closest Service'] = DfInt[
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20,
21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39,
40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58,
59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77,
78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96,
97, 98, 99, 100, 101, 102, 103, 104, 105, 106, 107, 108, 109, 110, 111,
112, 113, 114, 115, 116, 117, 118, 119, 120, 121, 122, 123, 124, 125, 126,
127, 128, 129, 130, 131, 132, 133, 134, 135, 136, 137, 138, 139, 140, 141,
142, 143, 144, 145, 146, 147, 148, 149, 150, 151, 152, 153, 154, 155, 156,
157, 158, 159, 160, 161, 162, 163, 164, 165, 166, 167, 168, 169, 170, 171,
172, 173, 174, 175, 176, 177, 178, 179, 180, 181, 182, 183, 184, 185, 186,
187, 188, 189, 190, 191, 192, 193, 194, 195, 196, 197, 198, 199, 200, 201,
202, 203, 204, 205, 206, 207, 208, 209, 210, 211, 212, 213, 214, 215, 216,
217, 218, 219, 220, 221, 222, 223]
].idxmin(axis=1)
by something like
DfInt['Closest Service'] = DfInt[[0:224]].idxmin(axis=1)
But this is not working... Anyone an idea?
If need select by labels use DataFrame.loc, : is for select all rows:
DfInt['Closest Service'] = DfInt.loc[:, :223].idxmin(axis=1)
If select by positions - first 223 columns use DataFrame.iloc by 224:
DfInt['Closest Service'] = DfInt.iloc[:, :224].idxmin(axis=1)
Suppose the image x consists of floats in the range [0, 1],
Torchvision adopts the transform of clip(x*255+0.5, 0, 255).as(uint8) .
Skimage seems similar to torch
TensorFlow uses an asymmetric approach
Details on the conversion follow below.
However, while investigating a few things, I found that this method gives an unfairly small chance for values of 0 and 255 compared to other values.
Why do these machine learning libraries use these unfair transformations?
pytorch
https://pytorch.org/vision/main/_modules/torchvision/utils.html#save_image
from collections import Counter, defaultdict
import numpy as np
DICT = defaultdict(list)
def as_uint8(X):
return np.clip(X * 255 + 0.5, 0, 255).astype(np.uint8)
for K, V in Counter(as_uint8(np.linspace(0/256, 256/256, 32 * 256))).items():
DICT[V].append(K)
print(DICT)
defaultdict(<class 'list'>, {17: [0, 255], 32: [1, 2, 3, 4, 5, 6, 7, 9, 10, 11, 12, 13, 14, 15, 17, 18, 19, 20, 21, 22, 23, 24, 26, 27, 28, 29, 30, 31, 32, 34, 35, 36, 37, 38, 39, 40, 42, 43, 44, 45, 46, 47, 48, 50, 51, 52, 53, 54, 55, 56, 57, 59, 60, 61, 62, 63, 64, 65, 67, 68, 69, 70, 71, 72, 73, 75, 76, 77, 78, 79, 80, 81, 83, 84, 85, 86, 87, 88, 89, 91, 92, 93, 94, 95, 96, 97, 98, 100, 101, 102, 103, 104, 105, 106, 108, 109, 110, 111, 112, 113, 114, 116, 117, 118, 119, 120, 121, 122, 124, 125, 126, 127, 128, 129, 130, 131, 133, 134, 135, 136, 137, 138, 139, 141, 142, 143, 144, 145, 146, 147, 149, 150, 151, 152, 153, 154, 155, 157, 158, 159, 160, 161, 162, 163, 164, 166, 167, 168, 169, 170, 171, 172, 174, 175, 176, 177, 178, 179, 180, 182, 183, 184, 185, 186, 187, 188, 190, 191, 192, 193, 194, 195, 196, 198, 199, 200, 201, 202, 203, 204, 205, 207, 208, 209, 210, 211, 212, 213, 215, 216, 217, 218, 219, 220, 221, 223, 224, 225, 226, 227, 228, 229, 231, 232, 233, 234, 235, 236, 237, 238, 240, 241, 242, 243, 244, 245, 246, 248, 249, 250, 251, 252, 253, 254], 33: [8, 16, 25, 33, 41, 49, 58, 66, 74, 82, 90, 99, 107, 115, 123, 132, 140, 148, 156, 165, 173, 181, 189, 197, 206, 214, 222, 230, 239, 247]})
skimage https://scikit-image.org/docs/dev/user_guide/data_types.html
from skimage.util import img_as_ubyte
from collections import Counter, defaultdict
import numpy as np
DICT = defaultdict(list)
for K, V in Counter(img_as_ubyte(np.linspace(0/256, 256/256, 32 * 256).reshape(-1, 1, 1)).reshape(-1)).items():
DICT[V].append(K)
print(DICT)
defaultdict(<class 'list'>, {17: [0, 255], 32: [1, 2, 3, 4, 5, 6, 7, 9, 10, 11, 12, 13, 14, 15, 17, 18, 19, 20, 21, 22, 23, 24, 26, 27, 28, 29, 30, 31, 32, 34, 35, 36, 37, 38, 39, 40, 42, 43, 44, 45, 46, 47, 48, 50, 51, 52, 53, 54, 55, 56, 57, 59, 60, 61, 62, 63, 64, 65, 67, 68, 69, 70, 71, 72, 73, 75, 76, 77, 78, 79, 80, 81, 83, 84, 85, 86, 87, 88, 89, 91, 92, 93, 94, 95, 96, 97, 98, 100, 101, 102, 103, 104, 105, 106, 108, 109, 110, 111, 112, 113, 114, 116, 117, 118, 119, 120, 121, 122, 124, 125, 126, 127, 128, 129, 130, 131, 133, 134, 135, 136, 137, 138, 139, 141, 142, 143, 144, 145, 146, 147, 149, 150, 151, 152, 153, 154, 155, 157, 158, 159, 160, 161, 162, 163, 164, 166, 167, 168, 169, 170, 171, 172, 174, 175, 176, 177, 178, 179, 180, 182, 183, 184, 185, 186, 187, 188, 190, 191, 192, 193, 194, 195, 196, 198, 199, 200, 201, 202, 203, 204, 205, 207, 208, 209, 210, 211, 212, 213, 215, 216, 217, 218, 219, 220, 221, 223, 224, 225, 226, 227, 228, 229, 231, 232, 233, 234, 235, 236, 237, 238, 240, 241, 242, 243, 244, 245, 246, 248, 249, 250, 251, 252, 253, 254], 33: [8, 16, 25, 33, 41, 49, 58, 66, 74, 82, 90, 99, 107, 115, 123, 132, 140, 148, 156, 165, 173, 181, 189, 197, 206, 214, 222, 230, 239, 247]})
tensorflow https://www.tensorflow.org/api_docs/python/tf/image/convert_image_dtype
import tensorflow as tf
from collections import Counter, defaultdict
import numpy as np
DICT = defaultdict(list)
img = tf.convert_to_tensor(np.linspace(0/256, 256/256, 32 * 256).reshape(-1, 1, 1))
img = tf.image.convert_image_dtype(img, dtype=tf.uint8, saturate=False)
img = tf.reshape(img, -1).numpy()
for K, V in Counter(img).items():
DICT[V].append(K)
print(DICT)
defaultdict(<class 'list'>, {33: [0, 17, 34, 51, 68, 85, 102, 119, 136, 153, 170, 187, 204, 221, 238], 32: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99, 100, 101, 103, 104, 105, 106, 107, 108, 109, 110, 111, 112, 113, 114, 115, 116, 117, 118, 120, 121, 122, 123, 124, 125, 126, 127, 128, 129, 130, 131, 132, 133, 134, 135, 137, 138, 139, 140, 141, 142, 143, 144, 145, 146, 147, 148, 149, 150, 151, 152, 154, 155, 156, 157, 158, 159, 160, 161, 162, 163, 164, 165, 166, 167, 168, 169, 171, 172, 173, 174, 175, 176, 177, 178, 179, 180, 181, 182, 183, 184, 185, 186, 188, 189, 190, 191, 192, 193, 194, 195, 196, 197, 198, 199, 200, 201, 202, 203, 205, 206, 207, 208, 209, 210, 211, 212, 213, 214, 215, 216, 217, 218, 219, 220, 222, 223, 224, 225, 226, 227, 228, 229, 230, 231, 232, 233, 234, 235, 236, 237, 239, 240, 241, 242, 243, 244, 245, 246, 247, 248, 249, 250, 251, 252, 253, 254], 17: [255]})
my suggestion 1
from collections import Counter, defaultdict
import numpy as np
DICT = defaultdict(list)
def as_uint8(X):
return np.clip(np.rint(X * 256 - 0.5), 0, 255).astype(np.uint8)
for K, V in Counter(as_uint8(np.linspace(0/256, 256/256, 32 * 256))).items():
DICT[V].append(K)
print(DICT)
defaultdict(<class 'list'>, {32: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99, 100, 101, 102, 103, 104, 105, 106, 107, 108, 109, 110, 111, 112, 113, 114, 115, 116, 117, 118, 119, 120, 121, 122, 123, 124, 125, 126, 127, 128, 129, 130, 131, 132, 133, 134, 135, 136, 137, 138, 139, 140, 141, 142, 143, 144, 145, 146, 147, 148, 149, 150, 151, 152, 153, 154, 155, 156, 157, 158, 159, 160, 161, 162, 163, 164, 165, 166, 167, 168, 169, 170, 171, 172, 173, 174, 175, 176, 177, 178, 179, 180, 181, 182, 183, 184, 185, 186, 187, 188, 189, 190, 191, 192, 193, 194, 195, 196, 197, 198, 199, 200, 201, 202, 203, 204, 205, 206, 207, 208, 209, 210, 211, 212, 213, 214, 215, 216, 217, 218, 219, 220, 221, 222, 223, 224, 225, 226, 227, 228, 229, 230, 231, 232, 233, 234, 235, 236, 237, 238, 239, 240, 241, 242, 243, 244, 245, 246, 247, 248, 249, 250, 251, 252, 253, 254, 255]})
my suggestion 2
from collections import Counter, defaultdict
import numpy as np
DICT = defaultdict(list)
def as_uint8(X):
return np.clip(X * 256, 0, 255).astype(np.uint8)
for K, V in Counter(as_uint8(np.linspace(0/256, 256/256, 32 * 256))).items():
DICT[V].append(K)
print(DICT)
defaultdict(<class 'list'>, {32: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99, 100, 101, 102, 103, 104, 105, 106, 107, 108, 109, 110, 111, 112, 113, 114, 115, 116, 117, 118, 119, 120, 121, 122, 123, 124, 125, 126, 127, 128, 129, 130, 131, 132, 133, 134, 135, 136, 137, 138, 139, 140, 141, 142, 143, 144, 145, 146, 147, 148, 149, 150, 151, 152, 153, 154, 155, 156, 157, 158, 159, 160, 161, 162, 163, 164, 165, 166, 167, 168, 169, 170, 171, 172, 173, 174, 175, 176, 177, 178, 179, 180, 181, 182, 183, 184, 185, 186, 187, 188, 189, 190, 191, 192, 193, 194, 195, 196, 197, 198, 199, 200, 201, 202, 203, 204, 205, 206, 207, 208, 209, 210, 211, 212, 213, 214, 215, 216, 217, 218, 219, 220, 221, 222, 223, 224, 225, 226, 227, 228, 229, 230, 231, 232, 233, 234, 235, 236, 237, 238, 239, 240, 241, 242, 243, 244, 245, 246, 247, 248, 249, 250, 251, 252, 253, 254, 255]})
I'm trying to convert fibonacci series to rgb image. so
import matplotlib.pyplot as plt
import numpy as np
N = int(input("Number of elements in Fibonacci Series, N, (N>=2) : "))
#starting elements: 0, 1
fibonacciSeries = [0,1]
if N>2:
for i in range(2, N):
nextElement = fibonacciSeries[i-1] + fibonacciSeries[i-2]
fibonacciSeries.append(nextElement)
print(fibonacciSeries)
fib_arr = np.array(fibonacciSeries)
fib_arr
img =np.zeros((100,100,4))
rgb = []
for i in fibonacciSeries:
rgb.append(i % 255)
print(rgb)
all this process ı have a list of mod each index of fib_Arr like that!
[0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 122, 100, 222, 67, 34, 101, 135, 236, 116, 97, 213, 55, 13, 68, 81, 149, 230, 124, 99, 223, 67, 35, 102, 137, 239, 121, 105, 226, 76, 47, 123, 170, 38, 208, 246, 199, 190, 134, 69, 203, 17, 220, 237, 202, 184, 131, 60, 191, 251, 187, 183, 115, 43, 158, 201, 104, 50, 154, 204, 103, 52, 155, 207, 107, 59, 166, 225, 136, 106, 242, 93, 80, 173, 253, 171, 169, 85, 254, 84, 83, 167, 250, 162, 157, 64, 221]
now how to convert this value to RGB image
I try to
plt.imshow(rgb)
plt.savefig("rgb.png")
but doesn't work
edited:
rgb_arr = np.array(rgb)
rgb_arr
array([ 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144,
233, 122, 100, 222, 67, 34, 101, 135, 236, 116, 97, 213, 55,
13, 68, 81, 149, 230, 124, 99, 223, 67, 35, 102, 137, 239,
121, 105, 226, 76, 47, 123, 170, 38, 208, 246, 199, 190, 134,
69, 203, 17, 220, 237, 202, 184, 131, 60, 191, 251, 187, 183,
115, 43, 158, 201, 104, 50, 154, 204, 103, 52, 155, 207, 107,
59, 166, 225, 136, 106, 242, 93, 80, 173, 253, 171, 169, 85,
254, 84, 83, 167, 250, 162, 157, 64])
from PIL import Image
img = Image.fromarray(rgb_arr, 'RGB')
img.save('test.png')
img.show()
picture
I have a list of ranges of numbers i.e. [[1, 3, 'C'], [4, 48, 'S'], [49, 54, 'C'], [55, 79, 'S'], [80, 83, 'C'], [84, 115, 'S'], [151, 152, 'C'], [153, 304, 'S'], [305, 305, 'D']].
I have a sequence of characters:
"MSEVTRSLLQRWGASLRRGADFDSWGQLVEAIDEYQILARHLQKEAQAQHNNSEFTEEQKKTIGKIATCLELRSAALQSTQSQEEFKLEDLKKLEPILKNILTYNKEFPFDVQPIPLRRILAPGEEENLEFEEDEEGGAGAGPPDSFSARVPGTLLPRLPSEPGMTLLTIRIEKIGLKDAGQCIDPYITVSVKDLNGIDLTPVQDTPVASRKEDTYVHFNVDIELQKHVERLTKGAAIFFEFKHYKPKKRFTSTKCFAFMEMDEIKPGPIVIELYKKPTDFKRKKLQLLTKKPLYLHLHQSLHKE"
and the numbers in the list correspond to the positions of the characters.
i.e. [1,3,'C'] means characters MSE (characters at position 1-3) are C.
Some of the characters in the string aren't being referenced by numbers in the list i.e. 116-150 aren't being referenced, and as a result I would like to remove these letters from the string.
Another example is when I have the range of numbers:[[45, 45, 'D'], [46, 49, 'C'], [50, 66, 'S'], [67, 101, 'C'], [102, 103, 'S'], [104, 106, 'C'], [107, 108, 'S'], [109, 120, 'C'], [121, 121, 'S'], [122, 123, 'C'], [124, 140, 'S'], [141, 149, 'C'], [150, 176, 'S'], [177, 178, 'C'], [179, 181, 'S'], [182, 194, 'C'], [195, 213, 'S'], [214, 217, 'C']]
This example is corresponding to a string of length 310, and we can see that the characters at position 0-45 are not being referred to, neither are the characters from 218-310 so I would like to remove these too.
I am having trouble creating a function that fixes these three cases of removal, as sometimes only one of the cases is true, or neither of them are true.
I have extracted the digits from the sequence into a list and changed the index as string index starts at 0:
[44, 44, 45, 48, 49, 65, 66, 100, 101, 102, 103, 105, 106, 107, 108, 119, 120, 120, 121, 122, 123, 139, 140, 148, 149, 175, 176, 177, 178, 180, 181, 193, 194, 212, 213, 216]
and my current thought process is:
1) If first number in list != 0, remove from corresponding character from 0 to element at first position - i.e. remove 0 to 45 in example 2.
2) If the difference between every 2nd and 3rd element != 1, i.e. if there is a gap in the middle of the numbers, then remove the letters corresponding to said elements. i.e. remove 116-150 mentioned above in example 1.
I am doing this by splitting the string into 0 to 116 and 150 to end of list and then concatenating the strings.
3) If the last number in the list is not the last character in the string, then remove from last character referenced until the end of the string.
I have tried implementing the above logic, and it works for some cases but not for others.
I have also tried adding the complete list of numbers into a list (i.e) the range of each pair of numbers, and removing the characters at the positions not referenced in list.
x is the list of numbers, strSeq is the string
copyx = x
x[0]=0
veryFirstElementOfX = int(x[0])
veryLastElementOfX = int(x[-1])
removed = 0
reeString = ''
if x[0] != 0:
for i in range(0, x[0]):
reeString = strSeq[x[0]:]
xMinusFirst = x
xMinusFirst.pop(0)
firstElement = int(x[0])
secondElement = int(x[1])
while secondElement != x[-1]:
x.pop(0)
x.pop(0)
firstElement = int(x[0])
secondElement = int(x[1])
difference = secondElement - firstElement
print("FE: " + str(firstElement))
print("SE: " + str(secondElement))
print(difference)
print("\n")
if difference > 1:
firstString = strSeq[0:firstElement]
secondString = strSeq[secondElement:]
reeString = reeString + firstString + secondString
removed = removed + (difference-1)
indexuno = veryFirstElementOfX
print(indexuno)
indexdos = veryLastElementOfX - removed
string1 = reeString[indexuno:indexdos]
I expect the output of a reduced string, with only the letters corresponding to the ranges of numbers in the list.
(i.e.) the characters whose position is not being referenced removed.
The above code works with the first example (i.e.) when characters at position 116-150 need to be removed.
But when I run the code for the second sequence, I get the following error:
7 x.pop(0)
8 firstElement = int(x[0])
----> 9 secondElement = int(x[1])
10 difference = secondElement - firstElement
11 print("FE: " + str(firstElement))
IndexError: list index out of range
Instead of finding what to delete, you could create a new string with what you want to keep.
s = "MSEVTRSLLQRWGASLRRGADFDSWGQLVEAIDEYQILARHLQKEAQAQHNNSEFTEEQKKTIGKIATCLELRSAALQSTQSQEEFKLEDLKKLEPILKNILTYNKEFPFDVQPIPLRRILAPGEEENLEFEEDEEGGAGAGPPDSFSARVPGTLLPRLPSEPGMTLLTIRIEKIGLKDAGQCIDPYITVSVKDLNGIDLTPVQDTPVASRKEDTYVHFNVDIELQKHVERLTKGAAIFFEFKHYKPKKRFTSTKCFAFMEMDEIKPGPIVIELYKKPTDFKRKKLQLLTKKPLYLHLHQSLHKE"
L = [[1, 3, 'C'], [4, 48, 'S'], [49, 54, 'C'], [55, 79, 'S'], [80, 83, 'C'],
[84, 115, 'S'], [151, 152, 'C'], [153, 304, 'S'], [305, 305, 'D']]
new_s = ''
for item in L:
new_s += s[item[0]-1:item[1]]
print(len(new_s), new_s)
Your problem statement is written a little bit confusing, but hopefully, the code below will give you some assistance or at least some ideas on how you can use sets in your logic to make your life much easier.
list_numbers = [
[1, 3, "C"],
[4, 48, "S"],
[49, 54, "C"],
[55, 79, "S"],
[80, 83, "C"],
[84, 115, "S"],
[151, 152, "C"],
[153, 304, "S"],
[305, 305, "D"],
]
# Generate a list of all the numbers given.
numbers = set()
for obj in list_numbers:
num1 = obj[0]
num2 = obj[1]
numbers.update(range(num1, num2 + 1))
# > {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99, 100, 101, 102, 103, 104, 105, 106, 107, 108, 109, 110, 111, 112, 113, 114, 115, 151, 152, 153, 154, 155, 156, 157, 158, 159, 160, 161, 162, 163, 164, 165, 166, 167, 168, 169, 170, 171, 172, 173, 174, 175, 176, 177, 178, 179, 180, 181, 182, 183, 184, 185, 186, 187, 188, 189, 190, 191, 192, 193, 194, 195, 196, 197, 198, 199, 200, 201, 202, 203, 204, 205, 206, 207, 208, 209, 210, 211, 212, 213, 214, 215, 216, 217, 218, 219, 220, 221, 222, 223, 224, 225, 226, 227, 228, 229, 230, 231, 232, 233, 234, 235, 236, 237, 238, 239, 240, 241, 242, 243, 244, 245, 246, 247, 248, 249, 250, 251, 252, 253, 254, 255, 256, 257, 258, 259, 260, 261, 262, 263, 264, 265, 266, 267, 268, 269, 270, 271, 272, 273, 274, 275, 276, 277, 278, 279, 280, 281, 282, 283, 284, 285, 286, 287, 288, 289, 290, 291, 292, 293, 294, 295, 296, 297, 298, 299, 300, 301, 302, 303, 304, 305}
print(numbers)
# Retrieve the biggest number.
max_number = max(numbers)
# Generate a set from 0 to max_number.
all_numbers = set(i for i in range(max_number))
# > {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99, 100, 101, 102, 103, 104, 105, 106, 107, 108, 109, 110, 111, 112, 113, 114, 115, 116, 117, 118, 119, 120, 121, 122, 123, 124, 125, 126, 127, 128, 129, 130, 131, 132, 133, 134, 135, 136, 137, 138, 139, 140, 141, 142, 143, 144, 145, 146, 147, 148, 149, 150, 151, 152, 153, 154, 155, 156, 157, 158, 159, 160, 161, 162, 163, 164, 165, 166, 167, 168, 169, 170, 171, 172, 173, 174, 175, 176, 177, 178, 179, 180, 181, 182, 183, 184, 185, 186, 187, 188, 189, 190, 191, 192, 193, 194, 195, 196, 197, 198, 199, 200, 201, 202, 203, 204, 205, 206, 207, 208, 209, 210, 211, 212, 213, 214, 215, 216, 217, 218, 219, 220, 221, 222, 223, 224, 225, 226, 227, 228, 229, 230, 231, 232, 233, 234, 235, 236, 237, 238, 239, 240, 241, 242, 243, 244, 245, 246, 247, 248, 249, 250, 251, 252, 253, 254, 255, 256, 257, 258, 259, 260, 261, 262, 263, 264, 265, 266, 267, 268, 269, 270, 271, 272, 273, 274, 275, 276, 277, 278, 279, 280, 281, 282, 283, 284, 285, 286, 287, 288, 289, 290, 291, 292, 293, 294, 295, 296, 297, 298, 299, 300, 301, 302, 303, 304}
print(all_numbers)
# Subtract our current list minus the set.
missing_numbers = all_numbers - set(numbers)
# > {0, 128, 129, 130, 131, 132, 133, 134, 135, 136, 137, 138, 139, 140, 141, 142, 143, 144, 145, 146, 147, 148, 149, 150, 116, 117, 118, 119, 120, 121, 122, 123, 124, 125, 126, 127}
print(missing_numbers)
If I understood your prompt right, with the list above you should be able to remove all the indexes in the string that is not mentioned.
This question already has answers here:
Understanding slicing
(38 answers)
Closed 7 years ago.
I have a python list of 300+ elements. I am trying to limit the print of the elements from the list, in order to show only 10 at a time.
Here is an example with only 5 elements inside a list, and I want to show just 2:
items = ["one", "two", "three", "four", "five"]
max_num = 2
for item in items:
# here I am not sure how I restrict the number elements from items
# do something
There are multiple ways of doing it.
1. (Pythonic way)
As #aa333 suggested:
for item in items[:max_num]:
print(item)
2. Probably a bit faster:
for i in xrange(max_num):
print(items[i])
3. Using a while loop:
counter = 0
while counter < max_num:
print(items[i])
counter += 1
Try this.
a = range(300)
for i in range(len(a)):
limit = 10;
i=(i*limit)
print a[i:(i+limit)]
if(i>(len(a)-limit-1)):
break;
This is the basic answer. I am sure better logic is available than this.
Output is like:
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
[11, 12, 13, 14, 15, 16, 17, 18, 19, 20]
[21, 22, 23, 24, 25, 26, 27, 28, 29, 30]......
Hope this is the desired output.
Simple way:
>>> m = 300
>>> k=range(m)
>>> s = 0
>>> e = 10
>>> while m > 0:
... print k[s:e]
... s+=10
... e+=10
... m-=10
...
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
[10, 11, 12, 13, 14, 15, 16, 17, 18, 19]
[20, 21, 22, 23, 24, 25, 26, 27, 28, 29]
[30, 31, 32, 33, 34, 35, 36, 37, 38, 39]
[40, 41, 42, 43, 44, 45, 46, 47, 48, 49]
[50, 51, 52, 53, 54, 55, 56, 57, 58, 59]
[60, 61, 62, 63, 64, 65, 66, 67, 68, 69]
[70, 71, 72, 73, 74, 75, 76, 77, 78, 79]
[80, 81, 82, 83, 84, 85, 86, 87, 88, 89]
[90, 91, 92, 93, 94, 95, 96, 97, 98, 99]
[100, 101, 102, 103, 104, 105, 106, 107, 108, 109]
[110, 111, 112, 113, 114, 115, 116, 117, 118, 119]
[120, 121, 122, 123, 124, 125, 126, 127, 128, 129]
[130, 131, 132, 133, 134, 135, 136, 137, 138, 139]
[140, 141, 142, 143, 144, 145, 146, 147, 148, 149]
[150, 151, 152, 153, 154, 155, 156, 157, 158, 159]
[160, 161, 162, 163, 164, 165, 166, 167, 168, 169]
[170, 171, 172, 173, 174, 175, 176, 177, 178, 179]
[180, 181, 182, 183, 184, 185, 186, 187, 188, 189]
[190, 191, 192, 193, 194, 195, 196, 197, 198, 199]
[200, 201, 202, 203, 204, 205, 206, 207, 208, 209]
[210, 211, 212, 213, 214, 215, 216, 217, 218, 219]
[220, 221, 222, 223, 224, 225, 226, 227, 228, 229]
[230, 231, 232, 233, 234, 235, 236, 237, 238, 239]
[240, 241, 242, 243, 244, 245, 246, 247, 248, 249]
[250, 251, 252, 253, 254, 255, 256, 257, 258, 259]
[260, 261, 262, 263, 264, 265, 266, 267, 268, 269]
[270, 271, 272, 273, 274, 275, 276, 277, 278, 279]
[280, 281, 282, 283, 284, 285, 286, 287, 288, 289]
[290, 291, 292, 293, 294, 295, 296, 297, 298, 299]
If I understood correctly the question, it could be done in the following way:
from math import floor
from numpy import arange
max_num=10.
lis=arange(0,404)
len_num=int(floor(float(len(lis))/max_num))
for i in range(len_num):
print lis[i*int(max_num):(i+1)*int(max_num)]
wait=input()
print(lis[(i+1)*int(max_num):])
This way, your list is shown slice by slice and the next slice isn't shown until the user presses one key