Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
200 views
in Technique[技术] by (71.8m points)

python - tableprint.table only works with randomized numbers

I'm trying to practice using tableprint by making a basic table, but I'm finding it difficult to input my own numbers (the only time I can make a table is when the numbers are randomized). Any help is appreciated.

import tableprint as tp
import numpy as np 

m=np.array([1,2,3,4])

n=np.random.randn(10, 4)
headers= ['name1', 'name2', 'name3', 'name4']
tp.table(n, headers) #works
tp.table(m, headers) #does not work
question from:https://stackoverflow.com/questions/65878890/tableprint-table-only-works-with-randomized-numbers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

tableprint.table takes a 2D array as first parameter, your array m is only 1D.

data : array_like An (m x n) array containing the data to print (m rows of n columns)

So you need to convert m to a 2D array:

tp.table(m[np.newaxis], headers)

Result:

╭───────┬───────┬───────┬───────╮
│ name1 │ name2 │ name3 │ name4 │
├───────┼───────┼───────┼───────┤
│     1 │     2 │     3 │     4 │
╰───────┴───────┴───────┴───────╯

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...