Formatting results of List in Python3 .4

regreading

I am trying to get the results of my list calculations to display in an orderly fashion by formatting the print() function. I have tried various methods from this site and Python Docs.

The closest that I can get it to display in a table like format is to align the output using the .format() function, but a couple of the lines are still offset. I understand that this is because the preceding results are longer than the others, but I can't figure out a way to get the text to align in columns.

When I try to amend the string formatting from what I already have I get a "tuple index out of range" error. But as far as I am aware I'm not using Tuples?

This is the string I am trying to print:

print('{:<10}'.format("Richter Scale Value:"), '{:<10}'.format(rVal),'{:^10}'.format ("Joules:"),'{:^10}'.format(joules),'{:>10}'.format("TNT:"),'{:>10}'.format(tnt))

But the output I get is:

Richter Scale Value: 1           Joules:   1995262.3149688789       TNT: 0.00047687913837688307
Richter Scale Value: 5           Joules:   1995262314968.8828       TNT: 476.87913837688404
Richter Scale Value: 9.1         Joules:   2.818382931264449e+18       TNT: 673609687.2046962
Richter Scale Value: 9.2         Joules:   3.981071705534953e+18       TNT: 951498973.5982201
Richter Scale Value: 9.5         Joules:   1.1220184543019653e+19       TNT: 2681688466.3048882

I can't figure out how to get the last three 'TNT' lines to match up.

This is the code preceding the print():

rVal = 0
qList=[1,5,9.1,9.2,9.5]
qList_len = len(qList)
for i in range (0,qList_len):
    rVal = qList[i]
    joules = 10**((1.5*rVal)+4.8)
    tnt = joules/(4.184*10**9)

Any assistance will be gratefully received thanks.

Martijn Pieters

You need to explicitly format your joules values a precision. You have only given a minimum width, but the default format exceeds that:

print('Richter Scale Value: {:<10.1f} Joules: {:<10.8g} TNT: {:10.8g}'.format(
    rVal, joules, tnt))

You may want to tweak the exact widths and how many decimals are displayed. You also could use f instead of g to always force displaying the values without scientific e notation, or use e to force scientific notation instead.

I combined your 6 str.format() calls into one. There is little point in formatting constant label strings.

この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。

侵害の場合は、連絡してください[email protected]

編集
0

コメントを追加

0

関連記事

分類Dev

Python3, re.match with list

分類Dev

Python3 - Nested list/dictionaries iteration

分類Dev

Python3 Create Array from List (I Think)

分類Dev

Getting the index of similar consecutive elements in list (Python3)

分類Dev

Python3 how to asyncio.gather() a list of partial functions

分類Dev

python3の[]とlist()の違い

分類Dev

appending a list from a read text file python3

分類Dev

Python 3でlist(range(4 ** 4 ** 4))を出力できません

分類Dev

Dapper Mapping List of Results

分類Dev

labeling results into List?

分類Dev

Time formatting in Python Pandas

分類Dev

python fstring with formatting

分類Dev

Json formatting, python parsing

分類Dev

PHP gives incorrect results when formatting long numbers

分類Dev

How to display a repeated list with angularjs and html formatting?

分類Dev

String formatting in python 3 without print function

分類Dev

Formatting dates for annotating count in Django + Python 3

分類Dev

Python multipying List within a list by 10 and getting different results when implementing hash table

分類Dev

PYTHON - List: Line 4 out of range

分類Dev

JSON formatting for 3 layer nest

分類Dev

Recurrence results stored in list in Prolog

分類Dev

optional space in python string formatting

分類Dev

Extract multiple lists of attributes from a list of objects with multiple attributes in python3

分類Dev

Python3 get error when constructing a map object by list constructor

分類Dev

python3の場合:list(iterables)の奇妙な動作

分類Dev

How to solve issue related to iterating over list in Python3 refactoring

分類Dev

Python3 list.count()の時間計算量

分類Dev

Python3:BeautifulSoup4が期待値を返さない

分類Dev

ANTLR4PLSQL文法Python3欠落関数

Related 関連記事

  1. 1

    Python3, re.match with list

  2. 2

    Python3 - Nested list/dictionaries iteration

  3. 3

    Python3 Create Array from List (I Think)

  4. 4

    Getting the index of similar consecutive elements in list (Python3)

  5. 5

    Python3 how to asyncio.gather() a list of partial functions

  6. 6

    python3の[]とlist()の違い

  7. 7

    appending a list from a read text file python3

  8. 8

    Python 3でlist(range(4 ** 4 ** 4))を出力できません

  9. 9

    Dapper Mapping List of Results

  10. 10

    labeling results into List?

  11. 11

    Time formatting in Python Pandas

  12. 12

    python fstring with formatting

  13. 13

    Json formatting, python parsing

  14. 14

    PHP gives incorrect results when formatting long numbers

  15. 15

    How to display a repeated list with angularjs and html formatting?

  16. 16

    String formatting in python 3 without print function

  17. 17

    Formatting dates for annotating count in Django + Python 3

  18. 18

    Python multipying List within a list by 10 and getting different results when implementing hash table

  19. 19

    PYTHON - List: Line 4 out of range

  20. 20

    JSON formatting for 3 layer nest

  21. 21

    Recurrence results stored in list in Prolog

  22. 22

    optional space in python string formatting

  23. 23

    Extract multiple lists of attributes from a list of objects with multiple attributes in python3

  24. 24

    Python3 get error when constructing a map object by list constructor

  25. 25

    python3の場合:list(iterables)の奇妙な動作

  26. 26

    How to solve issue related to iterating over list in Python3 refactoring

  27. 27

    Python3 list.count()の時間計算量

  28. 28

    Python3:BeautifulSoup4が期待値を返さない

  29. 29

    ANTLR4PLSQL文法Python3欠落関数

ホットタグ

アーカイブ