错误和输出问题的Python矩阵? [英] error and output issues for python matrix?
问题描述
我正在构建一个函数,它接受一个由列表组成的列表(例如: [['a'],['b'],['c']]
)并将其输出为表格。我不能使用漂亮的表格,因为我需要一个特定的输出(例如 | a | b |
),其中的行和空格完全相同。
下面是我的函数:
$ p $ def $ show_table(table):
如果表格是None:
table = []
new_table =
在行范围(表格)中:
在行中的
:
new_table + =(|+ val +)
new_table + =| \\\
return new_table.strip(\\\
)
/ pre>
我一直收到错误:
$ p $ show_table([ ['a'],['b'],['c']])
Traceback(最近一次调用的最后一个):
在< module>中的第1行文件< stdin> ;
文件< stdin>第5行,在show_table中
TypeError:'list'对象不能被解释为整数
我不确定为什么会有问题。我也得到一个输出错误,它只输出第一个列表中的第一个项目,没有更多。有人可以解释如何使用格式函数来摆脱这个错误,并输出我想要正确的?
固定的错误,但仍然失败的测试:
FAIL:test_show_table_12( main .AllTests)
Traceback最近一次调用的最后一次):
文件testlshp_12中的第116行testl7.py def test_show_table_12(self):self.assertEqual(show_table([['10','2','300 '],['4000','50','60'],['7','800','90000']]),'| 10 | 2 | 300 | \\\
| 4000 | 50 | 60 | \\\
| 7 | 800 | 90000 | \\\
')
AssertionError:'| 10 | 2 | 300 | \\\
| 4000 | 50 | 60 | \\\
| 7 | 800 | 90000 |'!='| 10 | 2 | 300 | \\\
| 4000 | 50 | 60 | \\\
| 7 | 800 | 90000 | \\\
'
- | 10 | 2 | 300 |
+ | 10 | 2 | 300 |
? +++ +++ +++
- | 4000 | 50 | 60 |
+ | 4000 | 50 | 60 |
? + ++ ++++
- | 7 | 800 | 90000 | + | 7 | 800 | 90000 |
? +++++++
问题发生在第5行:
pre $ 用于范围(表格)中的行:
...所以在这一行上的东西是尝试,没有成功,将其他解释为一个整数。如果我们查看<$ c的文档, $ c $> range(),我们看到这个:
lockquote
范围构造函数的参数必须是整数或者内置 int
或者实现 __ index __
特殊方法的任何对象)。
...但 table
不是整数;这是一个列表。如果你想遍历一个列表(或类似的东西),你不需要一个特殊的函数 - 只需
$ $ p $ 在范围内:
可以正常工作。
<除了滥用
range()
之外,你的函数还有另一个问题,那就是你缩减了太多的代码。这: 如果表格是None:
table = []
new_table =
表示行中的行(表):
行中的val:
new_table + =(|+ val +)
new_table + =| \\\
...如果表格
table = []
案子。解决这两个问题给你这个:$ p $
def show_table(table):
如果table是None:
table = []
new_table =
用于表中的行:
用于val中的行:
new_table + =(|+ val +)
new_table + =| \\\
return new_table.strip(\\\
)
(我也将所有的缩进改为了四个空格,并在这里和那里添加空格,以改善样式)。
I am building a function that takes a list made up of lists (ex: [['a'],['b'],['c']]
) and outputs it as a table. I cannot use pretty table because I need a specific output (ex | a | b |
) with the lines and the spaces exactly alike.
Here is my function:
def show_table(table):
if table is None:
table=[]
new_table=""
for row in range(table):
for val in row:
new_table+= ("| "+val+" ")
new_table+= "|\n"
return new_table.strip("\n")
I keep getting the error:
show_table([['a'],['b'],['c']])
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<stdin>", line 5, in show_table
TypeError: 'list' object cannot be interpreted as an integer
I'm not sure why there is an issue. I've also gotten an output error where it only outputs the first item in the first list and nothing more. Could someone explain how to use the format function to get rid of this error and output what I want correctly?
Fixed error but still failing tests:
FAIL: test_show_table_12 (main.AllTests)
Traceback (most recent call last):
File "testerl7.py", line 116, in test_show_table_12
def test_show_table_12 (self): self.assertEqual (show_table([['10','2','300'],['4000','50','60'],['7','800','90000']]),'| 10 | 2 | 300 |\n| 4000 | 50 | 60 |\n| 7 | 800 | 90000 |\n')
AssertionError: '| 10| 2| 300|\n| 4000| 50| 60|\n| 7| 800| 90000|' != '| 10 | 2 | 300 |\n| 4000 | 50 | 60 |\n| 7 | 800 | 90000 |\n'
- | 10| 2| 300|
+ | 10 | 2 | 300 |
? +++ +++ +++
- | 4000| 50| 60|
+ | 4000 | 50 | 60 |
? + ++ ++++
- | 7| 800| 90000|+ | 7 | 800 | 90000 |
? ++++ + + +
Your traceback tells you that the problem occurs on line 5:
for row in range(table):
… so something on that line is trying, without success, to interpret something else as an integer. If we take a look at the docs for range()
, we see this:
The arguments to the range constructor must be integers (either built-in
int
or any object that implements the__index__
special method).
… but table
is not an integer; it's a list. If you want to iterate over a list (or something similar), you don't need a special function – simply
for row in range:
will work just fine.
There's another problem with your function apart from the misuse of range()
, which is that you've indented too much of your code. This:
if table is None:
table=[]
new_table=""
for row in range(table):
for val in row:
new_table+= ("| "+val+" ")
new_table+= "|\n"
… will only execute any of the indented code if table
is None
, whereas what you really want is just to set table=[]
if that is the case. Fixing up both those problems gives you this:
def show_table(table):
if table is None:
table=[]
new_table = ""
for row in table:
for val in row:
new_table += ("| " + val + " ")
new_table += "|\n"
return new_table.strip("\n")
(I've also changed all your indents to four spaces, and added spaces here and there, to improve the style).
这篇关于错误和输出问题的Python矩阵?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!