从for循环Python打印数字列表作为数组
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了从for循环Python打印数字列表作为数组相关的知识,希望对你有一定的参考价值。
使用下面的代码,它会逐个打印值“phase”。我试图将这些值打印为for循环外的数组。
import math
Period = 6.2
time1 = datafile1[:,0]
magnitude1 = datafile1[:,1]
for i in range(len(time1)):
print(i,time1[i])
floor = math.floor((time1[i]-time1[0])/Period)
phase = ((time1[i]-time1[0])/Period)-floor
print (phase)
像这样打印:
0.002
0.003
0.004
0.005
我希望它像这样打印:
[0.002, 0.003, 0.004, 0.005]
答案
这将是该结果的最小修改要求路径
result = []
time1 = datafile1[:,0]
magnitude1 = datafile1[:,1]
for i in range(len(time1)):
result.append(i,time1[i])
floor = math.floor((time1[i]-time1[0])/Period)
phase = ((time1[i]-time1[0])/Period)-floor
result.append(phase)
print(result)
另一答案
在这里,我已经做到了,而不是printing
您的结果,您将它们附加到列表,然后打印出完整列表。
import math
Period = 6.2
time1 = datafile1[:,0]
magnitude1 = datafile1[:,1]
my_list = []
for i in range(len(time1)):
my_list.append(i,time1[i])
floor = math.floor((time1[i]-time1[0])/Period)
phase = ((time1[i]-time1[0])/Period)-floor
my_list.append(phase)
print(my_list)
另一答案
你可以做
import math
Period = 6.2
time1 = datafile1[:,0]
magnitude1 = datafile1[:,1]
list_to_print = []
for i in range(len(time1)):
print(i,time1[i])
floor = math.floor((time1[i]-time1[0])/Period)
phase = ((time1[i]-time1[0])/Period)-floor
list_to_print.append(phase)
print (list_to_print)
以上是关于从for循环Python打印数字列表作为数组的主要内容,如果未能解决你的问题,请参考以下文章
python 用for循环输入数字1-7判断输出相应星期几?