以简单格式打印数字表

时间:2016-07-10 16:10:12

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

我试图在Python中打印出一个看起来像这样的表(第一个数字表示数字量,第二个表示列数):

>>> print_table(13,4)
 0  1  2  3
 4  5  6  7
 8  9 10 11
12 13

有谁知道实现这个目标的方法?

3 个答案:

答案 0 :(得分:1)

这比最初听起来要困难一些。

var scheduleArray = [];

//blah blah removed code...everything works up to this point

casper.thenEvaluate(function(scheduleArray){

    console.log("##Your schedule is " + document.querySelector('form + div table').textContent );
    var rawSchedule = document.querySelector('form + div table').textContent;
    scheduleArray = rawSchedule.match(/((Monday)|(Tuesday)|(Wednesday)|(Thursday)|(Friday)|(Saturday)|(Sunday))([0-9]{1,2}\/[0-9]{1,2}\/[0-9]{4})((5)|(C6)|(6)|(7H)|(7F)|(715)|(8F)|(10F)|(12F)|(1F)|(2H)|(C2)|(2))/gi);
    console.log("##scheduleArray");
    console.log(scheduleArray);

    for (i=0;i<scheduleArray.length;i++){
        console.log(scheduleArray[i]);
    }


},scheduleArray);

casper.then(function(scheduleArray){
    console.log("##scheduleArray");
//This loop contains no data
    for (i=0;i<scheduleArray.length;i++){
            console.log(scheduleArray[i]);
        }
},scheduleArray);

答案 1 :(得分:0)

def numbers(a,b):

 i=0;
 c=0;
 while i<=a:
  print(i,end="") #prevents printing a new line
  c+=1
  if c>=b:
   print("\n") #prints a new line when the number of columns is reached and then reset the current column number
   c=0;

我认为应该有效

答案 2 :(得分:0)

def num2(n=10, r=3):
    print('\n'.join(' '.join(tuple(map(str, range(n+1)))[i:i+r]) for i in range(0, n+1, r)))

<<<
0 1 2
3 4 5
6 7 8
9 10