0

Just a problem I'm having and can't seem to figure out.

I have two tables: table1 and table2:

table1=[1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0] 

and

table2=[0 for i in range (21)] 

I want to run:

import random for d1 in range(21): table2[d1] = random.expovariate(gamma_val) 

But if value in table1 = 1, that position in table2 should run random.expovariate(x), and if not 1 run random.expovariate(y).

4 Answers 4

2

If I understand correctly would you not just:

import random for d1 in range(21): if table1[d1]: #1 evaluates to true in python table2[d1] = random.expovariate(x) else: table2[d1] = random.expovariate(y) 
Sign up to request clarification or add additional context in comments.

Comments

1
table2 = [] for d1 in table1: value = x if d1 == 1 else y table2.append(random.expovariate(value)) 

Comments

1

Another option could be:

import random options = [y, x] for d1 in range(21): table2[d1] = random.expovariate(options[table1[d1]]) 

I'm using the values (0 or 1) as indexes of the options list.

Comments

1

If you want table2 to be the same size as table1, you can (and should) use something like this:

table2 = [random.expovariate(x if d1 else y) for d1 in table1] 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.