-1

I have this small code code which is generating one element at every iteration. There is also one label being generated for each element.

count = 0
while (count<10):
    random.seed()
    tau = int(math.ceil(np.random.uniform(lorange, hirange)))
    count = count+1
    print('For', count,'th iter tau is', tau)

    X = [amplitude * np.exp(-t / tau)]
    print('And X is', X)

This gives me -

For 1 th iter tau is 253
 And X is [-8.3319888120435905]
For 2 th iter tau is 733
 And X is [-8.5504619226105234]
For 3 th iter tau is 6
 And X is [-1.637157007733484]
For 4 th iter tau is 35
 And X is [-6.5137386619958191]
For 5 th iter tau is 695
 And X is [-8.544086302536952]
For 6 th iter tau is 987
 And X is [-8.5805340921808924]
For 7 th iter tau is 807
 And X is [-8.5611651675760001]
For 8 th iter tau is 820
 And X is [-8.5628471889130697]
For 9 th iter tau is 799
 And X is [-8.5601030426343065]
For 10 th iter tau is 736
 And X is [-8.5509374123154984]

Now I want to get a list a list containing all the X values.

I checked the stack question here and did this-

myList = []
myList.append([X for _ in range(no_tau)])
print(myList)

But instead of getting all the X elements as a list, I am getting same element (the last X value) for multiple times.

[[[-8.5509374123154984], [-8.5509374123154984], [-8.5509374123154984], [-8.5509374123154984], [-8.5509374123154984], [-8.5509374123154984], [-8.5509374123154984], [-8.5509374123154984], [-8.5509374123154984], [-8.5509374123154984]]]

How can I get all the X elements in list instead of the same ones multiple times?

Also I want to later write this list in .csv format. Any help will be appreciated. Thanks

Community
  • 1
  • 1
zerogravty
  • 423
  • 1
  • 6
  • 21

1 Answers1

2

X is a list with a single element. Then when you do:

myList.append([X for _ in range(no_tau)])

you are adding that list with the only element (the last one) len(range(no_tau)) times.

You need to add it when you iterate. Something like this:

count = 0
myList = []
while (count<10):
    tau = int(math.ceil(np.random.uniform(lorange, hirange)))
    count = count+1
    X = amplitude * np.exp(-t / tau)
    myList.append(X)

You don't need that x was a list, sino un float. For that you need remove the []

Lucas
  • 6,869
  • 5
  • 29
  • 44
  • you mean `myList[-1]`? See the [Python tutorial](https://docs.python.org/3.6/tutorial/introduction.html#lists). You deleted the question :S – Lucas Jan 17 '17 at 05:07