Print in one line dynamically

后端 未结 20 3194
梦谈多话
梦谈多话 2020-11-21 23:32

I would like to make several statements that give standard output without seeing newlines in between statements.

Specifically, suppose I have:

for it         


        
20条回答
  •  野趣味
    野趣味 (楼主)
    2020-11-22 00:02

    If you just want to print the numbers, you can avoid the loop.

    # python 3
    import time
    
    startnumber = 1
    endnumber = 100
    
    # solution A without a for loop
    start_time = time.clock()
    m = map(str, range(startnumber, endnumber + 1))
    print(' '.join(m))
    end_time = time.clock()
    timetaken = (end_time - start_time) * 1000
    print('took {0}ms\n'.format(timetaken))
    
    # solution B: with a for loop
    start_time = time.clock()
    for i in range(startnumber, endnumber + 1):
        print(i, end=' ')
    end_time = time.clock()
    timetaken = (end_time - start_time) * 1000
    print('\ntook {0}ms\n'.format(timetaken))
    

    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 took 21.1986929975ms

    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 took 491.466823551ms

提交回复
热议问题