【问题标题】:Append function appends list sometime as row and sometimes as column附加函数有时将列表附加为行,有时附加为列
【发布时间】:2018-10-11 03:25:53
【问题描述】:

我正在读取一个包含一组序列号的文件。我将每个序列号分组到一个列表中,然后将它们添加到更大的列表中。我发现 append 函数有时将数字分组为一行,而另一些则为一列

[[366.0, 367.0, 368.0, 369.0, 370.0, 371.0, 372.0, 373.0],

[473.0, 474.0, 475.0, 476.0, 477.0, 478.0, 479.0, 480.0, 481.0],

[693.0, 694.0, 695.0, 696.0],

[718.0, 719.0, 720.0, 721.0, 722.0, 723.0],

[1036.0,

1037.0,

1038.0,

1039.0,

1040.0,

1041.0,

1042.0,

1043.0,

1044.0,

1045.0],

[1080.0, 1081.0, 1082.0, 1083.0, 1084.0, 1085.0, 1086.0, 1087.0],

[1424.0, 1425.0, 1426.0, 1427.0, 1428.0, 1429.0, 1430.0, 1431.0],

...

Here is the link for the datafile

任何想法。 谢谢

import numpy as np
i4whole=np.loadtxt('MJO_events.txt')

j=0
uu=[]
uu_final=[]
for i in np.arange(len(i4whole)-1):
    if ((i4whole[i+1]-i4whole[i]) == 1):
        #print(i4whole[i])
         uu.append(i4whole[i])
    else: 
        uu.append(i4whole[i]) # print the last number in the groub
        uu_final.append(uu)
        uu=[]
        j=j+1 #  to count the number of  group
uu.append(i4whole[i+1])
uu_final.append(uu)

【问题讨论】:

    标签: python python-3.x python-2.7 list


    【解决方案1】:

    没有区别。列表不是“行”或“列”;它们只是列表,无论在显示它们时使用什么空格来直观地排列它们的元素。您看到的是 IPython(可能是 IPython,无论如何)显示长列表,每行一个元素以提高可读性。

    【讨论】:

    • 我发现通过简单的打印如下所有子列表都很好地打印出来看起来像行。 for i in uu_final: print (i)
    猜你喜欢
    • 1970-01-01
    • 2019-04-29
    • 2019-05-13
    • 1970-01-01
    • 2020-07-26
    • 2016-08-25
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多