Related
I am trying to send random data to API using python flask with intervals of 1 second. But it only shows the last array of data. I am using the following code:
import time
import random
import datetime
from flask import Flask
mylist = []
ct = datetime.datetime.now()
app = Flask(__name__)
#app.route('/')
def index():
mylist = []
ct = datetime.datetime.now()
for i in range(0, 61):
x = random.randint(1, 100)
mylist.append(x)
if len(mylist) == 11:
right_in_left_out = mylist.pop(0)
else:
right_in_left_out = None
time.sleep(1)
print(mylist)
return mylist
if __name__ == "__main__":
app.run(debug=True)
OUTPUT:
WARNING: This is a development server. Do not use it in a production deployment. Use a production WSGI server instead.
* Running on http://127.0.0.1:5000
Press CTRL+C to quit
* Restarting with stat
* Debugger is active!
* Debugger PIN: 516-689-025
[50]
[50, 61]
[50, 61, 47]
[50, 61, 47, 63]
[50, 61, 47, 63, 24]
[50, 61, 47, 63, 24, 92]
[50, 61, 47, 63, 24, 92, 18]
[50, 61, 47, 63, 24, 92, 18, 75]
[50, 61, 47, 63, 24, 92, 18, 75, 95]
[50, 61, 47, 63, 24, 92, 18, 75, 95, 4]
[61, 47, 63, 24, 92, 18, 75, 95, 4, 40]
[47, 63, 24, 92, 18, 75, 95, 4, 40, 88]
[63, 24, 92, 18, 75, 95, 4, 40, 88, 39]
[24, 92, 18, 75, 95, 4, 40, 88, 39, 47]
[92, 18, 75, 95, 4, 40, 88, 39, 47, 58]
[18, 75, 95, 4, 40, 88, 39, 47, 58, 82]
[75, 95, 4, 40, 88, 39, 47, 58, 82, 88]
[95, 4, 40, 88, 39, 47, 58, 82, 88, 7]
[4, 40, 88, 39, 47, 58, 82, 88, 7, 90]
[40, 88, 39, 47, 58, 82, 88, 7, 90, 65]
[88, 39, 47, 58, 82, 88, 7, 90, 65, 93]
[39, 47, 58, 82, 88, 7, 90, 65, 93, 9]
[47, 58, 82, 88, 7, 90, 65, 93, 9, 55]
[58, 82, 88, 7, 90, 65, 93, 9, 55, 48]
[82, 88, 7, 90, 65, 93, 9, 55, 48, 83]
[88, 7, 90, 65, 93, 9, 55, 48, 83, 96]
[7, 90, 65, 93, 9, 55, 48, 83, 96, 63]
[90, 65, 93, 9, 55, 48, 83, 96, 63, 8]
[65, 93, 9, 55, 48, 83, 96, 63, 8, 43]
[93, 9, 55, 48, 83, 96, 63, 8, 43, 49]
[9, 55, 48, 83, 96, 63, 8, 43, 49, 95]
[55, 48, 83, 96, 63, 8, 43, 49, 95, 92]
[48, 83, 96, 63, 8, 43, 49, 95, 92, 43]
[83, 96, 63, 8, 43, 49, 95, 92, 43, 57]
[96, 63, 8, 43, 49, 95, 92, 43, 57, 91]
[63, 8, 43, 49, 95, 92, 43, 57, 91, 61]
[8, 43, 49, 95, 92, 43, 57, 91, 61, 27]
[43, 49, 95, 92, 43, 57, 91, 61, 27, 66]
[49, 95, 92, 43, 57, 91, 61, 27, 66, 70]
[95, 92, 43, 57, 91, 61, 27, 66, 70, 4]
[92, 43, 57, 91, 61, 27, 66, 70, 4, 34]
[43, 57, 91, 61, 27, 66, 70, 4, 34, 11]
[57, 91, 61, 27, 66, 70, 4, 34, 11, 95]
[91, 61, 27, 66, 70, 4, 34, 11, 95, 71]
[61, 27, 66, 70, 4, 34, 11, 95, 71, 35]
[27, 66, 70, 4, 34, 11, 95, 71, 35, 4]
[66, 70, 4, 34, 11, 95, 71, 35, 4, 98]
[70, 4, 34, 11, 95, 71, 35, 4, 98, 18]
[4, 34, 11, 95, 71, 35, 4, 98, 18, 81]
[34, 11, 95, 71, 35, 4, 98, 18, 81, 87]
[11, 95, 71, 35, 4, 98, 18, 81, 87, 84]
[95, 71, 35, 4, 98, 18, 81, 87, 84, 37]
[71, 35, 4, 98, 18, 81, 87, 84, 37, 63]
[35, 4, 98, 18, 81, 87, 84, 37, 63, 42]
[4, 98, 18, 81, 87, 84, 37, 63, 42, 18]
[98, 18, 81, 87, 84, 37, 63, 42, 18, 79]
[18, 81, 87, 84, 37, 63, 42, 18, 79, 28]
[81, 87, 84, 37, 63, 42, 18, 79, 28, 12]
[87, 84, 37, 63, 42, 18, 79, 28, 12, 36]
[84, 37, 63, 42, 18, 79, 28, 12, 36, 23]
[37, 63, 42, 18, 79, 28, 12, 36, 23, 49]
127.0.0.1 - - [21/Sep/2022 12:55:46] "GET / HTTP/1.1" 200 -
OUTPUT AT API:
I am looking to send data the same way as it is being displayed in the IDE with 1 sec intervals.
The problem lies here
if len(mylist) == 11:
right_in_left_out = mylist.pop(0)
Once this code executes for the first time, your list size is back to 10 , further iterations everytime it becomes 11 and then back to 10!
Your code is returning a list, and that is logical. If you want to return all the lists like the ones you displayed, you have to store them in a list of lists.
I mean by that:
all_lists = []
mylist = []
ct = datetime.datetime.now()
for i in range(0, 61):
x = random.randint(1, 100)
mylist.append(x)
if len(mylist) == 11:
mylist.pop(0)
time.sleep(1)
all_lists.append(mylist)
print(mylist)
return all_lists
There is also no need to use right_in_left_out variable in your code.
hello and thank you for any help I need help
I have this code
he reads the 5-10 lines but I need to remake it
f=open("input.txt", encoding='utf-8')
lines=f.readlines()
for x in range(5,10):
print(lines[x])
I am looking for this output
first input second is output image
input
1-100 lines
im search this number search in list
xx = lambda n:n and list(range(n,100,9))
print(xx(num))
output lines is text
[1, 11, 21, 31, 41, 51, 61, 71, 81, 91]
[2, 12, 22, 32, 42, 52, 62, 72, 82, 92]
[3, 13, 23, 33, 43, 53, 63, 73, 83, 93]
[4, 14, 24, 34, 44, 54, 64, 74, 84, 94]
[5, 15, 25, 35, 45, 55, 65, 75, 85, 95]
[6, 16, 26, 36, 46, 56, 66, 76, 86, 96]
[7, 17, 27, 37, 47, 57, 67, 77, 87, 97]
[8, 18, 28, 38, 48, 58, 68, 78, 88, 98]
[8, 18, 28, 38, 48, 58, 68, 78, 88, 98]
[9, 19, 29, 39, 49, 59, 69, 79, 89, 99]
[10, 20, 30, 40, 50, 60, 70, 80, 90,100]
just make a square
from 1 to 10 down from 11 to 20 down etc ...
there is text on the lines so that, for example, all 10 texts are combined into one
[1, 11, 21, 31, 41, 51, 61, 71, 81, 91]
this is just an example I have a long text of 33 thousand lines
Add 10 to the list index to get the contents of the line 10 rows later.
with open("input.txt", encoding='utf-8') as f:
lines = [line.rstrip() for line in f.readlines()]
lines = [line.rstrip() for line in lines]
for i in range(10):
output_line = ",".join(lines[i+n] for n in range(0, 100, 10))
print(output_line)
I have a range in a list(100 strings). I want to iterate a number of times (say 4 times) over this range by selecting 30 strings at a given interval and store them in a list. But I also want to store the selections outside the 30 strings and store them in a different list using pyspark.
Assuime the integers in the lists below are strings, Example code:
t=[]
start=[10,20,50,70]
for i in start:
windoww=i+30
for j in range(i,windoww):
t.append(j)
The above code gets me a list t ranging 10 to 99 with the start string specified in start.
I also want a list that is outside t. So for the first iteration if the content of t is
t= [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]
I want another list say u that has
u=[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 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]
Is this possible in pyspark?
I tried creating a list from [0 to 100] called v and did:
u=[stri for stri in v not in t]
However, I get an error that
'bool' object is not iterable
Try below function
def Diff(li1, li2):
return (list(list(set(li1)-set(li2)) + list(set(li2)-set(li1))))
li1 = [10, 15, 20]
li2 = [20, 15, 35]
li3 = Diff(li1, li2)
print(li3)
Output = [10,35]
I am looking for a way to reshape the following 1d-numpy array:
# dimensions
n = 2 # int : 1 ... N
h = 2 # int : 1 ... N
m = n*(2*h+1)
input_data = np.arange(0,(n*(2*h+1))**2)
The expected output should be reshaped into (2*h+1)**2 blocks of shape (n,n) such as:
input_data.reshape(((2*h+1)**2,n,n))
>>> array([[[ 0 1]
[ 2 3]]
[[ 4 5]
[ 6 7]]
...
[[92 93]
[94 95]]
[[96 97]
[98 99]]]
These blocks finally need to be reshaped into a (m,m) matrix so that they are stacked in rows of 2*h+1 blocks:
>>> array([[ 0, 1, 4, 5, 8, 9, 12, 13, 16, 17],
[ 2, 3, 6, 7, 10, 11, 14, 15, 18, 19],
...
[80, 81, 84, 85, 88, 89, 92, 93, 96, 97],
[82, 83, 86, 87, 90, 91, 94, 95, 98, 99]])
My problem is that I can't seem to find proper axis permutations after the first reshape into (n,n) blocks. I have looked at several answers such as this one but in vain.
As the real dimensions n and h are quite bigger and this operation takes place in an iterative process, I am looking for an efficient reshaping operation.
I don't think you can do this with reshape and transpose alone (although I'd love to be proven wrong). Using np.block works, but it's a bit messy:
np.block([list(i) for i in input_data.reshape( (2*h+1), (2*h+1), n, n )])
array([[ 0, 1, 4, 5, 8, 9, 12, 13, 16, 17],
[ 2, 3, 6, 7, 10, 11, 14, 15, 18, 19],
[20, 21, 24, 25, 28, 29, 32, 33, 36, 37],
[22, 23, 26, 27, 30, 31, 34, 35, 38, 39],
[40, 41, 44, 45, 48, 49, 52, 53, 56, 57],
[42, 43, 46, 47, 50, 51, 54, 55, 58, 59],
[60, 61, 64, 65, 68, 69, 72, 73, 76, 77],
[62, 63, 66, 67, 70, 71, 74, 75, 78, 79],
[80, 81, 84, 85, 88, 89, 92, 93, 96, 97],
[82, 83, 86, 87, 90, 91, 94, 95, 98, 99]])
EDIT: Never mind, you can do without np.block:
input_data.reshape( (2*h+1), (2*h+1), n, n).transpose(0, 2, 1, 3).reshape(10, 10)
array([[ 0, 1, 4, 5, 8, 9, 12, 13, 16, 17],
[ 2, 3, 6, 7, 10, 11, 14, 15, 18, 19],
[20, 21, 24, 25, 28, 29, 32, 33, 36, 37],
[22, 23, 26, 27, 30, 31, 34, 35, 38, 39],
[40, 41, 44, 45, 48, 49, 52, 53, 56, 57],
[42, 43, 46, 47, 50, 51, 54, 55, 58, 59],
[60, 61, 64, 65, 68, 69, 72, 73, 76, 77],
[62, 63, 66, 67, 70, 71, 74, 75, 78, 79],
[80, 81, 84, 85, 88, 89, 92, 93, 96, 97],
[82, 83, 86, 87, 90, 91, 94, 95, 98, 99]])
In python, I have an interval
a = 1-100
And I have a list with intervals
b = [11-20,41-50,91-110]
Now I would like my output to be like this in python:
a-b = 1-10,21-40,51-90
You have this :
a=range(1,100)
b = [range(11,20),range(41,50),range(91,110)] #list of ranges
So you can try :
d=[]
i=a.start
for sub_range in b: #for each sub range
#get the start and stop
start=sub_range.start
end=sub_range.stop
#append range to list
d.append(range(i,start-1))
i=end+1
print(d)
[range(1, 10), range(21, 40), range(51, 90)]
You can try this
a = range(1,101)
set(a)
b = [11,12,13,14,15,16,17,18,19,20,41,42,43,44,45,46,47,48,49,50,91,92,93,94,95,96,97,98,99,100]
set(a)-set(b)
{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 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}
You can use more_itertoools to divide the list
import more_itertools as mit
iterable = list(d)
[list(group) for group in mit.consecutive_groups(iterable)]
Result
[[1, 2, 3, 4, 5, 6, 7, 8, 9, 10], [21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40], [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]]