-1
import random
#import matplotlib.pyplot as plt

a1 = ['','','?','']
a2 = [10,25,43.34,90]

i = 0
i_array = []
while i < 10:
    i_array.append(i)
    i = i + 1
    r = random.random()
    for i, j in enumerate(a1):
        if j == '?':
            print(a2[i]*r)
            a3 = a2[i]*r

plt.line(r,a3)

The question mark I have in a1 could be in any place out of those four places. So, the value corresponding to it in a2 needs to be changed. the answer: import random #import matplotlib.pyplot as plt

a1 = ['','','?','']
a2 = [10,25,43.34,90]
xarray=[]
yarray=[]
i = 0
i_array = []#probably can delete this, I don't see any reason for it
for i in range(0,10):#use a for loop instead
    i_array.append(i)
    r = random.random()
    a3 = a2[a1.index('?')]*r#index here instead of the for loop
    print(a3)#since your assigning a3 anyway, might as well print that
    xarray.append(r)#plot needs arrays
    yarray.append(a3)
plt.plot(xarray,yarray)#plot your arrays
Albert Rothman
  • 998
  • 2
  • 9
  • 27
Ted
  • 37
  • 9

1 Answers1

1

Can you elaborate on what you are trying to do here? It seems like you are trying to choose a value in a2 based off where the '?' is contained in a1, and then multiply a2[index of ? in a1] by a random number and graph it with the product on the y axis and the random number on the x axis. Based on that assumption, there are several options. The most obvious would be to use the index() method, see this question:Python: finding an element in an array. Alternatively, if the '?' is meant to also be randomly placed in a1 then it is simpler to find the index of a2 randomly rather than using the two lists. Do this with the following a2[random.ranint(0, len(a2)-1)]. (documentation here: https://docs.python.org/2/library/random.html) Additionally, I'm not an expert with pyplot, but it looks like your call to plt.line(r,a3) might not work as you want it to. Based on what I think you want to do, you probably want to append r and a3 to two separate list(eg rlist, a3list) on each iteration of the loop and then call plt.plot(rlist, a3list). Finally, your while loop isn't wrong, but you seem to be using it as a for loop, so you might as well just do that instead(for i in range(0,10):)

Community
  • 1
  • 1
Albert Rothman
  • 998
  • 2
  • 9
  • 27
  • It seems like you are trying to choose a value in a2 based off where the '?' is contained in a1, and then multiply a2[index of ? in a1] by a random number and graph it with the product on the y axis and the random number on the x axis. <---- Yes, this is exactly what I am trying to do. I am not sure how to do this at all. Could you please help a little further. – Ted Apr 15 '16 at 18:34
  • I think I got it. But, I may get back. I used: `for i, j in enumerate(a1): if j == '?': print(a2[i])` Haven't tried it inside the loop yet – Ted Apr 15 '16 at 18:43
  • I updated the code. I have an infinite loop. How do I fix that ? – Ted Apr 15 '16 at 18:52
  • I think you are overcomplicating this; you are essentially writing code for a function that already exists. As I said above, you probably just want to use the index() function: a2[a1.index('?')] – Albert Rothman Apr 15 '16 at 18:53
  • Can you tell me where exactly I need to put that then because I don't know. – Ted Apr 15 '16 at 18:54
  • How do I get all the 10 values together so that I can graph them? – Ted Apr 15 '16 at 18:58
  • i_array = [] arraythatyouneedtoadd=[] secondarraythatyoushouldadd=[] for i in range(0, 10): i_array.append(i)#not sure what this line is for r = random.random() print(a2[a1.index('?')]*r) a3 = a2[a1.index('?')]*r arraythatyouneedtoadd.append(r) secondarraythatyoushouldadd.append(a3) plt.line(arraythatyouneedtoadd,secondarraythatyoushouldadd) – Albert Rothman Apr 15 '16 at 18:59
  • whoops, that should have been a code block; anyway, you can try copy pasting that and making sense of it, but please reread my answer above: forget the second for loop altogether and use a1.index('?'). Then make some new arrays and append a3 and r to them. – Albert Rothman Apr 15 '16 at 19:01
  • as for getting the ten values together, that's why you need append to separate arrays, eg newarray=[], newarray.append(a3) plt.plot(newarray,) – Albert Rothman Apr 15 '16 at 19:03
  • could you please made that a code block. I have no clue what it means – Ted Apr 15 '16 at 19:11
  • added an edit, it should have the exact solution. ps. if you like the answer, please accept it(in addition to the edits) :) – Albert Rothman Apr 15 '16 at 19:19
  • The graph looks good. But, it isn't clear at all.I don't want to arrange the random values from lower to higher, and then graph it or however it is doing it. I want it to go in sequence. I have a straight line, and that is incorrect. – Ted Apr 15 '16 at 19:30
  • It should look have a shape like the one this page with lines and markers (orange): https://plot.ly/python/line-and-scatter/ – Ted Apr 15 '16 at 19:37
  • I'm not sure what you mean, but you can try adding an optional parameter to the plot function that will plot a line between you points. see the tutorial here: http://matplotlib.org/users/pyplot_tutorial.html. calling plot with '-' will give you dashes between your points. – Albert Rothman Apr 15 '16 at 19:52