0

I’m trying to create a loop to check if a number in one array is 1 or 0, and then depending on this run some if statements using values from a different array which, depending on the if, will then append the original 1/0 array and run the loop again. the 1/0 array's first value has to be 1 so the code can work, and then add as it goes on, so the code would be adding to the array as it was running if that makes sense? like an excel spreadsheet uses the cell above in order to calculate a value as shown.

im very new to python and feel like there should be a very simple solution to this but i cant find one anywhere.

This is what im trying to do using python

My attempt

import random as rd
import numpy as np
import pandas as pd


pww = float(input("Pww Value: \n"))
pdd = float(input("Pdd Value: \n"))
pwd = float(1 - pww)
pdw = float(1 - pdd)
lda = float(input("Lambda Value:"))

random = np.random.rand(3649)
print(random)

state = np.array([1])

for x in state:
    if x ==1:
        for y in random:
            if y < pww:
                np.append(state, [1])
            else:
                np.append(state, [0])
    if x == 0:
        for y in random:
            if y <pdd:
                np.apend(state, [0])
            else:
                np.append(state, [1])


print(state)

any help is greatly appreciated.

i have looked at other posts which recommend enumerating the original array but that hasnt helped. other advice has been using the zip command but i dont really understand what that actually does - i couldnt get it to work anyway.

Lawrence
  • 49
  • 5
  • 5
    [Please do not upload images of code/errors/text.](//meta.stackoverflow.com/q/285551) Include it as a [formatted code block](/help/formatting) instead of an image. – Pranav Hosangadi Mar 14 '23 at 17:06
  • Once you've done the first iteration through the outer loop, `for x in state:`, what should happen next? Quit the outer loop, or continue with the altered `state` array. If the latter, from where in `state` should the loop continue? – 9769953 Mar 14 '23 at 17:18
  • @9769953 the loop should continue with the new altered state, it should then restart the loop for the new 1/0 that will be appended - hope this is clear, thanks – Lawrence Mar 14 '23 at 17:21
  • You realize that this exponentially increases `state`? You're appending 3649 values to `state` in the first iteration, and the same in the next iteration, so `state` grows with 3649 values each time. That'll just blow up your memory. – 9769953 Mar 14 '23 at 17:25
  • It is generally advised that you [not modify the loop you are iterating over](https://stackoverflow.com/a/6260097/442945), as this can cause undefined behavior. – Nathaniel Ford Mar 14 '23 at 17:37

2 Answers2

1

You could access the last value of your state list as you go through the random values. It can be done in a loop or with the extend method and a comprehension:

import random

pww = 0.2
pdd = 0.3
pwd = 1-pww
pdw = 1-pdd

rnd = [random.random() for _ in range(10)]

state = [1]
state.extend( int(r<pww) if state[-1] else int(r<pdd) for r in rnd)

print(*map("{:3.2}".format,rnd))
print(state)

# 0.36 0.18 0.36 0.38 0.71 0.9 0.78 0.019 0.92 0.61
# [1, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0]

The zip() function allows you to get paired values for corresponding positions in two lists. You could use it to get the 1/0 from state in parallel with the random probabilities in rnd. You could also use the 1/0 values as an index to select the value to compare (instead of using if/else):

state = [1]
state.extend( int(r<[pdd,pww][s]) for s,r in zip(state,rnd))
Alain T.
  • 40,517
  • 4
  • 31
  • 51
-1

It seems like you want to add to the list as you iterate through it. You could do something like this:

some_list = [0, 1, 2, 3, 4]
i = 0  
while i < len(some_list):  
  #do your stuff here  
  if(i == 4): #Example:can change this to whatever you want to check
      some_list.append(5)
  if(i == 5): #Only true if the last if had been executed
      #Illustrating that the loop while loop through new values in list
      some_list.append(6)
  i += 1

print(some_list)
#output: [0, 1, 2, 3, 4, 5, 6]

This is an example, replace the if statements with whatever you are trying to check, it's a little unclear in the post but it seems you want something like: if(some_list[i] == 1) or if(some_list[i] == 0)

The while loop checks the condition every iteration, and because the length is increasing, it will just keep looping until the loop condition is met.

mrblue6
  • 587
  • 2
  • 19
  • This is an extremely complicated way of writing `some_list.extend([5, 6])` - no input value will alter that outcome, unless the list is not long enough to hit `4` or `5`. Further, despite the OP's code, they are not interested in `if (i == 4)` so much as they are interested in `if (some_list[i] == 4)`. – Nathaniel Ford Mar 14 '23 at 17:48
  • @NathanielFord The `i ==4` and `i ==5` are just examples. I don't see why you couldn't just replace that with `if (some_list[i] == 4)` to get the output you want – mrblue6 Mar 14 '23 at 18:10
  • If that was your intent, you should include it in your question. Regardless, you don't want to do this - modifying a list you're iterating over is an antipattern. – Nathaniel Ford Mar 14 '23 at 19:46