用python换行打印的问题

时间:2018-07-19 16:33:49

标签: python

我是python的新手,以下代码在分配i值时遇到问题

centriod_array如下所示

[115117112101 95 95 76 65 78 69 83 95 71 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 99 83 87 191 55 98 111 76

92 117 112 100 114 95 110 98 105 99 105 88 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 92 96 99 101 69 111 88 97

88 92 112 101 114 95 110 98 105 99 103 88 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 92 96 97 101 69 111 98 88 ]

def get_tx (centriods,cnt,flag):
for i in centriods:
    if i == " ":
       cnt = cnt+1
       if (cnt == 30 and cnt <= 37) :
         tx = i
         print (tx)



  k=0
    while k < 3:
        get_tx (centriod_array[k],0,0)
        k = k+1


it is printing as below:

9
9 

8
3 

8
7 

1
9
1 

5
5 

9
8 

1
1
1 

7
6

....


>> i want it to be print like this 

99 83 87 191 55 98 111 76
92 96 99 101 69 111 88 97
92 96 97 101 69 111 98 88 



>> Basically i'm trying to print the values after "0".

Please help!!

以下实际上是数组中的确切元素,例如。抱歉造成混乱

'2 60 109 117 108 116 105 45 99 108 111 99 107 62 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 115 109 117 95 115 109 117 105 111 95 116 46 65 86 70 83 49 95 112 115 109 97 118 102 115 95 103 97 116 101 114 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 100 102 95 116 99 100 120 99 95 116 51 46 80 115 109 65 118 102 115 95 71 97 116 101 114 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 70 67 76 75 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0\n'

我想提取片段并将它们分配给如下所示的变量:

60 109 117 108 116 105 45 99 108 111 99 107 62 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
115 109 117 95 115 109 117 105 111 95 116 46 65 86 70 83 49 95 112 115 109 97 118 102 115 95 103 97 116 101 114 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
100 102 95 116 99 100 120 99 95 116 51 46 80 115 109 65 118 102 115 95 71 97 116 101 114 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
70 67 76 75 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0

其他具有非零到非零转换的数组(在这里我以粗体突出显示)

'2 100 120 105 111 95 115 101 114 100 101 115 95 107 112 120 95 103 48 95 116 46 67 76 78 84 49 95 80 72 89 **48 95** 112 108 108 49 51 103 95 100 119 111 114 100 99 108 107 0 0 0 115 117 112 101 114 95 110 98 105 111 48 46 110 98 105 111 95 112 99 105 101 95 116 120 114 120 95 116 48 46 82 83 77 85 95 80 67 73 69 48 95 114 111 109 115 116 114 97 112 95 83 84 82 65 80 95 66 73 70 95 82 69 86 69 82 83 69 95 65 76 76 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 115 117 112 101 114 95 110 98 105 111 48 46 112 99 105 101 95 112 99 115 95 116 48 46 77 65 67 95 80 67 83 95 112 105 112 101 48 95 111 99 82 101 113 49 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 82 69 70 67 76 75 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0'

请帮助!

1 个答案:

答案 0 :(得分:1)

这可以按您期望的方式工作;利用rsplit中每个项目上的centroid_array来检索最后一个0之后的值:

centroid_array = ['115 117 112 101 95 95 76 65 78 69 83 95 71 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 99 83 87 191 55 98 111 76', 
                  '92 117 112 100 114 95 110 98 105 99 105 88 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 92 96 99 101 69 111 88 97', 
                  '88 92 112 101 114 95 110 98 105 99 103 88 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 92 96 97 101 69 111 98 88']

for x in centroid_array:
    print(x.rsplit(' 0 ')[-1])

# 99 83 87 191 55 98 111 76
# 92 96 99 101 69 111 88 97                                          
# 92 96 97 101 69 111 98 88

根据注释中的要求,检索从第50个空格到第200个空格的数字:

def get_tx(centriods):
    for i, x in enumerate(centriods.split()):
        if i >= 50 and i < 200:
            print(x, end=' ')

get_tx(s)

或者简单地:

print(' '.join(s.split()[50:200]))

其中s是您的输入。