I want to print letters and numbers alternately on Python.

Asked 1 years ago, Updated 1 years ago, 149 views

  1 #!/usr/bin/env python
  2
  3 import os
  4 import subprocess
  5 import sys
  6 import shlex
  7 import matplotlib.pyplot as plt
  8 import re
  9
 10 def runCommand(command):
 11     popenArgs = shlex.split(command)
 12     process = subprocess.Popen(popenArgs, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
 13     out, err = process.communicate()
 14     return [out.rstrip(), err.rstrip()]
 15
 16 if __name__ == '__main__':
 17     nTimes = input("Enter nTimes: ")
 18     values = [ [], [], [], [], [], [], [], [], [] ]
 19     for index in xrange(nTimes):
 20         output, errorOutput = runCommand('./getMultipleHits.sh')
 21         outputSplit = re.split(' : |\n', output)
 22         #split up the output
 23         i = 2
 24         j = 0
 25         for ggg in outputSplit:
 26             if j == 9: break
 27             values[j].append(outputSplit[i])
 28             i += 3
 29             j += 1
 30     sumValues = []
 31     averages = []
 32     k = 0
 33     sumValue = 0
 34     for index in values:
 35         for value in values[k]:
 36             sumValue += float(value)
 37         sumValues.append(sumValue)
 38         averages.append(sumValue/nTimes)
 39         k += 1
 40     l = 0
 41     while l < 9:
 42         print('Sum of values of tsf' + str(l) + str(sumValues[l]))
 43         print('Average of values of tsf' + str(l) + str(averages[l]))
 44         m = 1
 45         plt.figure()
 46         for value in values[l]:
 47             plt.plot(m, value, 'ro')
 48             m += 1
 49         plt.axis([0, nTimes+1, 0, 2147500000])
 50         l += 1
 51     plt.show()

First of all, this is the code. There are output functions on the 42nd and 43rd lines, which are currently output as follows:

Sum of values of tsf02078497898.0
Average of values of tsf01039248949.0
Sum of values of tsf16028065745.0
Average of values of tsf13014032872.5
Sum of values of tsf28631865954.0
Average of values of tsf24315932977.0
Sum of values of tsf311328958215.0
Average of values of tsf35664479107.5
Sum of values of tsf414357615824.0
Average of values of tsf47178807912.0
Sum of values of tsf516788316306.0
Average of values of tsf58394158153.0
Sum of values of tsf619636037707.0
Average of values of tsf69818018853.5
Sum of values of tsf721343543817.0
Average of values of tsf710671771908.5
Sum of values of tsf823736480671.0
Average of values of tsf811868240335.5

I'd like to print it out as below. The number is the resulting value, but now it's written randomly.

Sum of values of tsf0:     16516131315
Average of values of tsf0: 15615631851
Sum of values of tsf1:     16516131315
Average of values of tsf1: 15615631851
...

Sum of values of tsf8:     16516131315
Average of values of tsf8: 15615631851

Now, there are two problems: printing letters and numbers alternately, and spacing. That is,

tsf58394158153.0

There should be a space between the numbers after 5 and 5. And I want to put ':' in between.

Help me

python print

2022-09-22 15:31

2 Answers

I think you don't you come in on the code to just study hard too

so tired I already know the answer.

Line 42 and 43

print('Sum of values of tsf' + str(l) + str(sumValues[l]))
print('Average of values of tsf' + str(l) + str(averages[l]))

print('Sum of values of tsf' + str(l) + ':\t' + str(sumValues[l]))
print('Average of values of tsf' + str(l) + ':\t' + str(averages[l]))

Change it to

For your information, \t is a tab and you can use it as much as you want

When I tried the hashcode code executor , it came out pretty when I did it twice on the 42nd line and once on the 43rd line


2022-09-22 15:31

Python supports format functionality for strings.

It's kind of a template-like function.

'{}-{}:{}'.format('one', 'two', 'three')
'one-two:three'

'Sum of values of {}:{}'.format('tsf0', '8394158153.0')
'Sum of values of tsf0:8394158153.0'

https://docs.python.org/3.4/library/string.html

See also Python Help.

Then try to fix it.


2022-09-22 15:31

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.