簡體   English   中英

在6列python上打印數字列表

[英]print a list of numbers on 6 columns python

我在一行中打印了這樣的列表或數字:0.232 2.34234 ... 1.2232。 總共有156個號碼。 我只想在一行中打印其中的6個:

a b c d e f

g h i j k l

...

到目前為止,我已經嘗試過了,我的號碼在line.dat中:

with open('line.dat') as file:
File = file.readlines()
for i in range(len(File)/6+1):
    print ''.join(File[i*6:(i+1)*6]) 

但是,這仍然在一行中打印數字。 任何人都可以幫我這個忙! 謝謝。

我可以在同一列中打印前52個數字,以此類推(還是6列)。 這次我有很多數字,我想將前52個數字保持在同一列中。 所以最后我有:

1 53 105 157 209 261

2

...

52104156208260312

313……………………

...(另外52個數字,依此類推)

您需要在空格上split數據,無法對字符進行切片,因為您將對數字部分進行切片,采用六個字符與采用六個數字並不相同,除非您知道每個數字子串的確切長度,那么您需要拆分為單獨的子元素:

with open('line.dat')) as f:
    line = f.read().split()
    print("\n".join([" ".join(line[i:i+6]) for i in xrange(0, len(line)-5, 6)]))
import itertools as it

with open('data') as f:
    lines = f.readlines()


k = lines[0].split()

i = iter(k)

for j in range(len(k) // 6):
    print(list(it.islice(i, 0, 6)))

如果文件行是range(156) 輸出

['[0,', '1,', '2,', '3,', '4,', '5,']
['6,', '7,', '8,', '9,', '10,', '11,']
['12,', '13,', '14,', '15,', '16,', '17,']
['18,', '19,', '20,', '21,', '22,', '23,']
['24,', '25,', '26,', '27,', '28,', '29,']
['30,', '31,', '32,', '33,', '34,', '35,']
['36,', '37,', '38,', '39,', '40,', '41,']
['42,', '43,', '44,', '45,', '46,', '47,']
['48,', '49,', '50,', '51,', '52,', '53,']
['54,', '55,', '56,', '57,', '58,', '59,']
['60,', '61,', '62,', '63,', '64,', '65,']
['66,', '67,', '68,', '69,', '70,', '71,']
['72,', '73,', '74,', '75,', '76,', '77,']
['78,', '79,', '80,', '81,', '82,', '83,']
['84,', '85,', '86,', '87,', '88,', '89,']
['90,', '91,', '92,', '93,', '94,', '95,']
['96,', '97,', '98,', '99,', '100,', '101,']
['102,', '103,', '104,', '105,', '106,', '107,']
['108,', '109,', '110,', '111,', '112,', '113,']
['114,', '115,', '116,', '117,', '118,', '119,']
['120,', '121,', '122,', '123,', '124,', '125,']
['126,', '127,', '128,', '129,', '130,', '131,']
['132,', '133,', '134,', '135,', '136,', '137,']
['138,', '139,', '140,', '141,', '142,', '143,']
['144,', '145,', '146,', '147,', '148,', '149,']
['150,', '151,', '152,', '153,', '154,', '155]']

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM