Home > Software design >  If the data is read 3 times in a row, then the next data changes value by 20 data
If the data is read 3 times in a row, then the next data changes value by 20 data

Time:09-21

I want to make a change if the randomList data produces a value of 0 three times in a row and then it will change the randomList value to 1 for the next 20 data and here's an example of a random program that I made.

import random
randomlist = []
for i in range(0,100):
    n = random.randint(1,20)
    if n > 10 :
        randomlist.append('0')
    else :
        randomlist.append('1')
    
print(randomlist)

I'm sorry I made changes to the data, to make it easier to understand my question.

dataList = [4, 9, 10, 12, 5, 6, 15, 16, 8, 18, 20, 21, 1, 3, 5, 6, 8, 15, 13, 22, 25, 1, 3, 11, 5, 18, 24, 25, 23, 19, 14, 6, 9, 3, 20, 3, 1, 20]
result=[]
for i in dataList:
    if i > 10 :
        result.append('0')
    else :
        result.append('1')

Here are the results

['1', '1', '1', '0', '1', '1', '0', '0', '1', '0', '0', '0', '1', '1', '1', '1', '1', '0', '0', '0', '0', '1', '1', '0', '1', '0', '0', '0', '0', '0', '0', '1', '1', '1', '0', '1', '1', '0']

Desired result

['1', '1', '1', '0', '1', '1', '0', '0', '1', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '1', '1', '0', '1', '1', '0']

CodePudding user response:

If i understand you right, you want to add 20 zeros to your list if the previous 3 elemenst were zero. Here i check the last three elements, and add zero 20 times then pass the next 20 iterations.

dataList = [4, 9, 10, 12, 5, 6, 15, 16, 8, 18, 20, 21, 1, 3, 5, 6, 8, 15, 13, 22, 25, 1, 3, 11, 5, 18, 24, 25, 23, 19, 14, 6, 9, 3, 20, 3, 1, 20]
result=[]
one_before = now = None
i = 0
for item in dataList:
    try:
        if dataList[i] > 10 : 
            result.append('0')
        else :
            result.append('1')
    except:
        break  

    two_before = one_before     # the two element before the last element
    one_before = now     # one element before the last element 
    now = result[-1]     # value of the last element
    zero_list = ['0']*20    # List containing 20 zeros to append result 

    if now=='0' and one_before=='0' and two_before=='0':  #Check if last three elements are zero
        result.extend(zero_list)  # Add 20 zeros 
        i = i 21    # Pass 20 iterations
    else:
        i = i 1     # Pass to next iteration

print(result)

CodePudding user response:

Here is another example that might help. result is transformed based on the first rule (> 10 or not) Then we go thru the list, as soon as we do have three 0 ("000"), we change the next 20 items by "0". If near the end of the dataList, you find another sequence of 0, then we change the values to end of the list (keep the original size of the dataList).

dataList = [4, 9, 10, 12, 5, 6, 15, 16, 8, 18, 20, 21, 1, 3, 5, 6, 8, 15, 13, 22, 25, 1, 3, 11, 5, 18, 24, 25, 23, 19, 14, 6, 9, 3, 20, 3, 1, 20]
result=['0' if x > 10 else '1' for x in dataList ]
print(result)
size, i = len(dataList), 2
while i<size:
    if result[i] result[i-1] result[i-2] == "000":
        NumberOf0= min(20, size - i-1)
        for j in range(1 NumberOf0):
            result[i j] = '0'
        i =j 2
    i =1
print (result)
  • Related