简体   繁体   English

在6列python上打印数字列表

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

I have a list or numbers printed like this in one line: 0.232 2.34234 ...1.2232. 我在一行中打印了这样的列表或数字:0.232 2.34234 ... 1.2232。 In total there are 156 numbers. 总共有156个号码。 I would like to print only 6 of them in a line like: 我只想在一行中打印其中的6个:

a b c d e f

g h i j k l

... ...

I have tried this so far, my numbers are in the line.dat: 到目前为止,我已经尝试过了,我的号码在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]) 

However, this is still printing the numbers in one line. 但是,这仍然在一行中打印数字。 Can anyone help me with this, please! 任何人都可以帮我这个忙! Thanks. 谢谢。

Can I print first 52 numbers in the same column and so on (still 6 columns). 我可以在同一列中打印前52个数字,以此类推(还是6列)。 I have lots of numbers this time and I want to keep the first 52 and so on numbers in the same column. 这次我有很多数字,我想将前52个数字保持在同一列中。 So in the end I have: 所以最后我有:

1 53 105 157 209 261 1 53 105 157 209 261

2 2

... ...

52 104 156 208 260 312 52104156208260312

313 ... ... ... ... ... 313……………………

...(another 52 numbers and so on) ...(另外52个数字,依此类推)

You need to split the data on whitespace, you cannot slice characters as you will slice off parts of numbers, taking six characters is not the same as taking six of your numbers, unless you knew the exact length of each number substring then you need to separate into individual sub elements with split: 您需要在空格上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)))

if file line is range(156) output 如果文件行是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