从for循环Python将数字列表打印为数组

时间:2018-09-26 03:27:02

标签: python arrays for-loop

使用下面的代码,它一一打印“相位”值。我正在尝试将这些值打印为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]

3 个答案:

答案 0 :(得分:2)

这是该结果的最少修改要求路径

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)

答案 1 :(得分:0)

在这里,我将结果而不是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)

答案 2 :(得分:0)

你可以

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)