如何从数据列表中创建表?

时间:2014-12-01 06:03:37

标签: python numpy

我有3个数据列表:时间,半径和角度。我想制作这三个列表的表,但不确定如何。我希望它看起来像:

time   radius   angle
0.0    3.1      0
0.1    3.0      3

等等。我相信我应该以某种方式制作一个阵列,但我不确定如何做到这一点。我尝试使用numpy.array或numpy.arange来计算它,但无法使用任何东西。

输入的示例可能是

print time
[ 0.          0.001001    0.002002    0.003003    0.004004    0.00500501]
print radius
[ 1.          1.00099904  1.00199417  1.00298539  1.00397268  1.00495605]
print angle
[  2.54558441e-01   5.08609013e-01   7.62155215e-01   1.01520052e+00    1.26774839e+00   1.51980226e+00]

另外,为了澄清,这些列表包含数百个值。

4 个答案:

答案 0 :(得分:1)

您可以像这样使用Zip:

>>> time = ['time',0.0,0.1]
>>> radius=['radius',3.1,3.0]
>>> angle=['angle',0,3]
>>> list = zip(time,radius,angle)
>>> list
[('time', 'radius', 'angle'), (0.0, 3.1, 0), (0.1, 3.0, 3)]

答案 1 :(得分:1)

我认为这应该有所帮助:

time_l = [0.  ,        0.001001,    0.002002 ,   0.003003,    0.004004,    0.00500501]
radious_l = [ 1.,          1.00099904 , 1.00199417  ,1.00298539 , 1.00397268,  1.00495605]
angle_l  = [  2.54558441e-01,   5.08609013e-01 ,  7.62155215e-01 ,  1.01520052e+00  ,  1.26774839e+00 ,  1.51980226e+00]



an_array = np.array([time_l, radious_l, angle_l]).T


np.set_printoptions(suppress = True, precision=3)

print(an_array)

结果:

[[ 0.     1.     0.255]
 [ 0.001  1.001  0.509]
 [ 0.002  1.002  0.762]
 [ 0.003  1.003  1.015]
 [ 0.004  1.004  1.268]
 [ 0.005  1.005  1.52 ]]
[[ 0.255  1.     0.   ]
 [ 0.509  1.001  0.001]
 [ 0.762  1.002  0.002]
 [ 1.015  1.003  0.003]
 [ 1.268  1.004  0.004]
 [ 1.52   1.005  0.005]]

答案 2 :(得分:0)

一种方法可能是

>>> times = ['time', 0.0, 0.1]
>>> radius = ['radius', 3.1, 3.0]
>>> angles = ['angle', 0, 3]
>>> data = [times, radius, angles]
>>> data
[['time', 0.0, 0.1], ['radius', 3.1, 3.0], ['angle', 0, 3]]
>>> table = zip(*data)
>>> for row in table:
...     print row
...
('time', 'radius', 'angle')
(0.0, 3.1, 0)
(0.1, 3.0, 3)

因为你说输入很大,你可以试试下面的

>>> import itertools
>>> for t,r,a in itertools.izip(times, radius, angles):
...     print t, r, a
...
time radius angle
0.0 3.1 0
0.1 3.0 3
>>> for t,r,a in itertools.izip_longest(times, radius, angles):
...     print t, r, a
...
time radius angle
0.0 3.1 0
0.1 3.0 3

可以找到文档here

答案 3 :(得分:0)

很难理解你的愿望,也许这会有所帮助?

time_list = [0.0,0.1]
radius_list = [3.1,3.0]
angle_list = [0,3]

tbl = []
for i in range(len(time_list)):
    temp_var = {}
    temp_var['time'] = time_list[i]
    temp_var['radius'] = radius_list[i]
    temp_var['angle'] = angle_list[i]
    tbl.append(temp_var)
print tbl

print "time\tradius\tangle"
for i in range(len(tbl)):
    print "%s\t%s\t%s" % (tbl[i]['time'],tbl[i]['radius'],tbl[i]['angle'])