Related
I've a python list as this one [2, 5, 26, 37, 45, 12, 23, 37, 45, 12, 23, 37, 45, 12, 23, 37]. The real list is really long. The list repeat itself after a certain point in this case after 37. I have no problem finding the number at which it repeats, but i need to truncate the list at the second one. In this case the result would be [2, 5, 26, 37, 45, 12, 23, 37]. For finding the number (37 in this case) i use a function firstDuplicate() found on stackoverflow. Someone can help me ?
def firstDuplicate(a):
aset = set()
for i in a:
if i in aset:
return i
else:
aset.add(i)
pass
pass
pass
LIST = LIST[1:firstDuplicate(LIST)]
You can use the same basic idea of firstDuplicate() and create a generator that yields values until the dupe is found. Then pass it to list(), a loop, etc.
l = [2, 5, 26, 37, 45, 12, 23, 37, 45, 12, 23, 37, 45, 12, 23, 37]
def partitionAtDupe(l):
seen = set()
for n in l:
yield n
if n in seen:
break
seen.add(n)
list(partitionAtDupe(l))
# [2, 5, 26, 37, 45, 12, 23, 37]
It's not clear what should happen if there are no dupes. The code above will yield the whole list in that case.
A function to find the period size and length of repeated numbers should start from the end of the sequence of numbers. This will make it easier to ensure that there is a cycle up to the end of the list and avoid any concerns over non-periodic repetitions at the beginning of the list.
For example:
def getPeriod(seq):
lastPos = { n:p for p,n in enumerate(seq) }
prevPos = { n:p for p,n in enumerate(seq) if p<lastPos[n] }
period = 1
for n in reversed(seq):
if n not in prevPos: break
delta = lastPos[n] - prevPos[n]
if delta%period == 0 or period%delta == 0:
period = max(delta,period)
else: break
nonPeriodic = (i for i,(n,p) in enumerate(zip(seq[::-1],seq[-period-1::-1])) if n != p)
periodLength = next(nonPeriodic,0)
return period, periodLength
output:
seq = [2, 5, 26, 37, 45, 12, 23, 37, 45, 12, 23, 37, 45, 12, 23, 37]
period, periodLength = getPeriod(seq)
print(period,periodLength) # 4 9
print(seq[:-periodLength]) # [2, 5, 26, 37, 45, 12, 23]
import random
list1 = [11, 22, 33, 44, 55]
for i in range(5):
random_number = random.randint(0,4)
print(list1[random_number])
I am trying to find a way to print an entire list randomly without printing duplicates of the list in the first loop. Then if I increase the range to 10, 15, 20, and so on, every 5 prints displays all the elements randomly and without duplicates(by duplicates i mean no duplicates for every group of 5 prints displayed) i.e. 22, 33, 11, 55, 44, 44, 11, 33, 22, 55, 33, 22, 55, 44, 11 ...
you could make a copy of the list, then pop elements out of the copy until it's empty, then 'reload' it by copying the original list again
import random
list1 = [11, 22, 33, 44, 55]
clonedList = list1.copy()
for i in range(5):
if len(clonedList == 0):
clonedList = list1.copy()
random_number = random.randint(0,len(clonedList)-1)
print(clonedList.pop(random_number))
I have a time signal (37913 ms to 40010) and I want to split it in every 20 ms gap.
For example for first 20 ms:
for t in time:
if t>=37913 and t< 37933:
list1.append(t)
This gives me the list [37913.496549, 37916.878267, 37918.506757].
I want to make several different lists on every 20 ms gap. I know it should be really simple but somehow I cannot think of a solution.
****Edited****
So to further explain my point, what I actually want to achieve is that, have an incoming acceleration (with no upper time limit) signal (Green Bars) and I want to check if these incoming samples are in the range 0-20 ms, 10-30 ms or 20-40 ms and so on. If they are in such an interval then I have to approximate points (black dots) using this data. for example, if current values are in between 0-20 ms then I can use all of these values to approximate value at 10 ms by some approximation (let's suppose there is no current value at 10 ms). Approximation method is not important right now, I just want to capture these values
Any help or suggestion is highly appreciated thanks to all in advance.
Here's something that I think does what you desire. To test it I had to create some sample input data since you don't have any in your question—which is what is going on at the very beginning of the snippet.
For each time range interval, it creates a separate "bucket" which contains the corresponding time values within the time range. Note that some time values may end up being place in two buckets since the interval ranges overlap.
from pprint import pprint
import random
random.seed(42) # Create same "random" sequence each run for testing.
# First create some test data.
INTERVAL = 0.02 # 20 ms
start = 37913
times = []
nvalues = 2, 3, 4, 2, 1 # Number of values in each interval.
for i, nvalue in enumerate(nvalues):
lowerbounds = start + i*INTERVAL
upperbounds = start + (i+1)*INTERVAL
for _ in range(nvalue):
times.append(random.uniform(lowerbounds, upperbounds))
print('There are {} time values:'.format(len(times)))
times.sort() # Put into ascending order.
pprint(times)
#=======
# Split the times up into "buckets" of values depending on their range.
HALF_INTERVAL = INTERVAL / 2
brackets = []
print()
print('Time brackets:')
st = int(min(times))
for i in range(4):
begin = round(st + i*HALF_INTERVAL, 6)
end = round(begin + INTERVAL, 6)
brackets.append((begin, end))
print(' ', begin, end)
buckets = [[] for _ in range(len(brackets))] # Create empty buckets.
for t in times: # Put each time in the cooresponding bucket of times.
for i, (begin, end) in enumerate(brackets):
if begin <= t <= end:
buckets[i].append(t)
print()
print('Stored in corresponding interval bucket:')
for i, bracket in enumerate(brackets):
print('bucket[{}]: range: {!r}, values: {}'.format(
i, bracket, buckets[i]))
Sample Output:
There are 12 time values:
[37913.0005002151,
37913.01278853597,
37913.02446421476,
37913.025500586366,
37913.03472942428,
37913.04173877665,
37913.048438436395,
37913.05353398975,
37913.05784359135,
37913.060595944386,
37913.064372759494,
37913.09010710576]
Time brackets:
37913.0 37913.02
37913.01 37913.03
37913.02 37913.04
37913.03 37913.05
Stored in corresponding interval bucket:
bucket[0]: range: (37913.0, 37913.02), values: [37913.0005002151, 37913.01278853597]
bucket[1]: range: (37913.01, 37913.03), values: [37913.01278853597, 37913.02446421476, 37913.025500586366]
bucket[2]: range: (37913.02, 37913.04), values: [37913.02446421476, 37913.025500586366, 37913.03472942428]
bucket[3]: range: (37913.03, 37913.05), values: [37913.03472942428, 37913.04173877665, 37913.048438436395]
If your list of timestamps is sorted:
from itertools import groupby
sample = range(100)
INTERVAL_SIZE = 20
key = lambda x: x // INTERVAL_SIZE
list(list(v) for k, v in groupby(sample, key=key))
Will give you:
[
[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]
]
If it is not sorted, add a sort (by key) before you use groupby, since it requires a sorted iterable.
I would suggest this method:
arrs = []
while len(arr) > 20:
pice = arr[:20]
arrs.append(pice)
arr = arr[20:]
arrs.append(arr)
you can make function out of it which gives you a piece each time you call it.
If I understand your question, you could use a range:
r=range(37913,40010,20)
output:
[37913, 37933, 37953, 37973, 37993, 38013, 38033, 38053, 38073, 38093,
38113, 38133, 38153, 38173, 38193, 38213, 38233, 38253, 38273, 38293,
38313, 38333, 38353, 38373, 38393, 38413, 38433, 38453, 38473, 38493,
38513, 38533, 38553, 38573, 38593, 38613, 38633, 38653, 38673, 38693,
38713, 38733, 38753, 38773, 38793, 38813, 38833, 38853, 38873, 38893,
38913, 38933, 38953, 38973, 38993, 39013, 39033, 39053, 39073, 39093,
39113, 39133, 39153, 39173, 39193, 39213, 39233, 39253, 39273, 39293,
39313, 39333, 39353, 39373, 39393, 39413, 39433, 39453, 39473, 39493,
39513, 39533, 39553, 39573, 39593, 39613, 39633, 39653, 39673, 39693,
39713, 39733, 39753, 39773, 39793, 39813, 39833, 39853, 39873, 39893,
39913, 39933, 39953, 39973, 39993]
If you don't mind using numpy, you can try this:
import numpy as np
#a = np.arange(37913,40010+1,1)
a = np.arange(0,10+1,1) # comment this and uncomment above for your case
nlen = 2
parts = np.array_split(a,int(np.ceil(len(a) / nlen)))
print("a = {}".format(a)) # [ 0 1 2 3 4 5 6 7 8 9 10]
print("len(a) = {}".format(len(a))) # 11
print("parts[0] = {}".format(parts[0])) # [0 1]
print("parts[-1] = {}".format(parts[-1])) # [10]
If you just want to break whole array into n parts, just do this:
nparts = 6
np.array_split(a,nparts)
Edit:
The desired behaviour of the program is to find the number sequences that have an increasing trend, so I want to generate from ks list a list like this:
desiredList=[[97,122],[98,111],[98,101,103,103,104]]
I have the following, my goal is to run the for loop based on the length of the list, the list length gets changed inside the for loop itself. Python takes into account only the length before the for loop, when the length of the list is changed in the loop it still takes the older value before the loop. Here is the code:
ks=[97,122,111,98,111,98,101,103,103,104,97]
splitLine=2
counter=[]
for i in range(0,len(ks)):
a=ks[i:splitLine]
while len(a)>1:
for j in range(0,len(a)):
m=j
n=j+1
if(a[m]-a[n]<=0):
c=c+1
k=splitLine+c-1
a.append(ks[k]) #When append happens, the for loop still takes the older value of len(a) instead of new value
else:
a.pop(-1)
counter.append(a)
splitLine=splitLine+1
a=[]
break
A quick fix for your looping problem would be to swap out your for loop for a while loop. Change this:
for j in range(0,len(a)):
# <loop contents>
to this:
j = 0
while j < len(a):
# <loop contents>
j += 1
The for loop is grabbing values of j out of a range (a list in Python 2, and a generator object in Python 3). This range is calculated when the for loop is run the first time; it will not update after that, no matter what you do to a.
The while loop gives you more control in this situation, because you can specify the condition under which you want to exit the loop.
Your implementation is probably nesting too many loops for the problem it is trying to solve.
This first implementation contains an error. See below for the fix.
Try something along these lines perhaps:
l = [97,122,111,98,111,98,101,103,103,104,97]
out = []
acc = []
for v in l:
if len(acc)==0 or v >= acc[-1]:
acc.append(v)
else:
if len(acc) > 1:
out.append(acc)
acc = [v]
print(out)
>>>[[97, 122], [98, 111], [98, 101, 103, 103, 104]]
That previous code is slow and can drop the last found fragment. I found that error while running random tests on it to try an optimized version. The following code shows the original code with the correction and the optimized version which can be 30% faster.
def original(l):
out = []
acc = []
added = False
for v in l:
if len(acc)==0 or v >= acc[-1]:
acc.append(v)
else:
added = False
acc = [v]
if acc is not None and len(acc)>1 and not added:
added = True
out.append(acc)
return out
def optimized(l):
out = []
acc = None
tmp = None
deb_v = False
for v in l:
prev = acc[-1] if (acc is not None and len(acc)) else tmp
if prev is not None and v >= prev:
if tmp is not None:
acc = []
acc.append(tmp)
out.append(acc)
tmp = None
acc.append(v)
else:
acc = None
tmp = v
return out
# The original test data
l = [97,122,111,98,111,98,101,103,103,104,97]
assert original(l) == optimized(l) == [[97,122],[98,111],[98,101,103,103,104]]
# A list that triggered last-fragment-dropped error
l = [57, 16, 6, 19, 40, 3, 4, 13, 2, 70, 85, 65, 32, 69, 54, 51, 95, 74, 92, 46, 45, 26, 0, 61, 99, 43, 67, 71, 97, 10, 18, 73, 88, 47, 33, 82, 25, 75, 93, 80, 23, 37, 87, 90, 49, 15, 35, 63, 17, 64, 5, 72, 89, 21, 50, 8, 41, 86, 31, 78, 52, 76, 56, 42, 77, 36, 11, 60, 39, 22, 68, 27, 24, 28, 59, 96, 29, 38, 12, 79, 53, 9, 83, 94, 34, 14, 7, 48, 30, 20, 66, 62, 91, 58, 81, 1, 98, 44, 55, 84]
assert original(l) == optimized(l)
# Random testing
import random
l = list(range(100))
random.shuffle(l)
assert original(l) == optimized(l)
# Timing!
import timeit
print(timeit.timeit("original(l)", globals={"l":l, "original": original}))
# 43.95869998800117
print(timeit.timeit("optimized(l)", globals={"l":l, "optimized": optimized}))
# 34.82134292599949
As Moinuddin says, the root of your problem isn't clear to us. However, the code below shows how you can keep iterating over a list as its length changes:
def iterate_for_static_list_length(l):
for i in range(len(l)):
yield i
l.append(object())
def iterate_for_dynamic_list_length(l):
for i, _ in enumerate(l):
yield i
l.append(object())
if __name__ == '__main__':
l = [object()] * 3
print('Static implementation')
for value in iterate_for_static_list_length(l):
input(value)
print('\nDynamic implementation')
for value in iterate_for_dynamic_list_length(l):
input(value)
Output
Static implementation
0
1
2
Dynamic implementation
0
1
2
3
4
5
6
7
8
This program will keep going forever. In your code I can see that you conditionally append to the list within the loop, so it seems like it should terminate eventually.
I have a simple script to generate UK lottery numbers (7 numbers, 1 to 49 inclusive).
My code has a function that generates 7 random numbers into a list, runs set on the list removing duplicate numbers, checks if there are still 7 members in the list, and if not the function calls itself to generate 7 new numbers.
However, when the function calls itself it does not return the list.
I'd appreciate knowing what I'm doing wrong here.
from random import randint
def lotto():
l = []
for r in range(1,8):
l.append(randint(1,49))
print "DEBUG: l=", l
print "DEBUG: set(l)=", set(l), len(set(l))
if(len(set(l)) !=7):
lotto()
else:
print "Before return l, l = ", l
return l
def main():
numbers = lotto()
print numbers
Here is a sample run that does not work correctly:
DEBUG: l= [44, 32, 12, 12, 33, 16, 31]
DEBUG: set(l)= set([32, 33, 44, 12, 16, 31]) 6
DEBUG: l= [46, 20, 10, 24, 16, 35, 44]
DEBUG: set(l)= set([35, 10, 44, 46, 16, 20, 24]) 7
Before return l, l = [46, 20, 10, 24, 16, 35, 44]
None
And a sample run that does work correctly:
DEBUG: l= [20, 5, 21, 37, 10, 44, 38]
DEBUG: set(l)= set([37, 38, 10, 44, 20, 21, 5]) 7
Before return l, l = [20, 5, 21, 37, 10, 44, 38]
[20, 5, 21, 37, 10, 44, 38]
You're not returning the result of the recursive call.
if(len(set(l)) !=7):
return lotto()
The recursive call
lotto()
does not actually return the value returned by lotto(). You'd need to use
return lotto()
instead. (Note that instead of the tail-recursive call, a loop would be preferable.)
That said, there is a much easier solution to your actual problem, namely random.sample(). Python2.x version:
import random
print random.sample(xrange(1, 50), 7)
Python 3.x version:
import random
print(random.sample(range(1, 50), 7))