[英]Creating python list with multiple elements and write to .csv
我有这个小的代码,每次迭代都会生成一个元素。 还存在用于每个元件产生一个标签。
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)
这给了我-
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]
现在,我想获取一个包含所有X值的列表。
myList = []
myList.append([X for _ in range(no_tau)])
print(myList)
但是,而不是让所有的X元素列表,我收到了多次相同的元素(最后X值)。
[[[-8.5509374123154984], [-8.5509374123154984], [-8.5509374123154984], [-8.5509374123154984], [-8.5509374123154984], [-8.5509374123154984], [-8.5509374123154984], [-8.5509374123154984], [-8.5509374123154984], [-8.5509374123154984]]]
如何获得列表中的所有X元素,而不是多次获得相同的元素?
我也想稍后以.csv格式编写此列表。 任何帮助将不胜感激。 谢谢
X是具有单个元素的列表。 然后,当您这样做时:
myList.append([X for _ in range(no_tau)])
您正在添加具有唯一元素(最后一个) len(range(no_tau))
次的列表。
迭代时需要添加它。 像这样:
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)
您不需要x是列表,sino un float。 为此,您需要删除[]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.