【发布时间】:2015-12-16 09:15:47
【问题描述】:
你好,我这里有个函数:
def cross_product(t1,t2):
new = []
#create new schema
for category in t2[0]:
t1[0].append(category)
new.append(t1[0])
#make table content
if t1 != t2:
for row in t1[1:]:
step = 0
for i in t2[1:]:
new.append(row + i)
if len(new) < 2:
return None
return new
按我的意愿工作。但是,当我使用这个 py.test 函数对其进行测试时,如果函数中有多个断言语句,我会不断收到断言错误。我检查了代码,根本不应该有断言错误......但我似乎无法修复它。谁能告诉我我能做些什么来解决这个问题?我仍处于学习 python 的初级阶段,所以尽量保持简单。
这是测试用例:
def test_cross_product_basic():
"""
Test cross product operation with basic/generic inputs.
"""
result_1 = [["Employee", "Department", "Department", "Head"],
["Smith", "sales", "production", "Mori"],
["Smith", "sales", "sales", "Brown"],
["Black", "production", "production", "Mori"],
["Black", "production", "sales", "Brown"],
["White", "production", "production", "Mori"],
["White", "production", "sales", "Brown"]]
assert is_equal(result_1, cross_product(R1, R2))
result_2 = [['Department', 'Head', 'Employee', 'Department'],
['production', 'Mori', 'Smith', 'sales'],
['production', 'Mori', 'Black', 'production'],
['production', 'Mori', 'White', 'production'],
['sales', 'Brown', 'Smith', 'sales'],
['sales', 'Brown', 'Black', 'production'],
['sales', 'Brown', 'White', 'production']]
assert is_equal(result_2, cross_product(R2, R1))
result_3 = [['Surname', 'FirstName', 'Age', 'Salary', 'Employee', 'Department'],
['Smith', 'Mary', 25, 2000, 'Smith', 'sales'],
['Smith', 'Mary', 25, 2000, 'Black', 'production'],
['Smith', 'Mary', 25, 2000, 'White', 'production'],
['Black', 'Lucy', 40, 3000, 'Smith', 'sales'],
['Black', 'Lucy', 40, 3000, 'Black', 'production'],
['Black', 'Lucy', 40, 3000, 'White', 'production'],
['Verdi', 'Nico', 36, 4500, 'Smith', 'sales'],
['Verdi', 'Nico', 36, 4500, 'Black', 'production'],
['Verdi', 'Nico', 36, 4500, 'White', 'production'],
['Smith', 'Mark', 40, 3900, 'Smith', 'sales'],
['Smith', 'Mark', 40, 3900, 'Black', 'production'],
['Smith', 'Mark', 40, 3900, 'White', 'production']]
assert is_equal(result_3, cross_product(EMPLOYEES, R1))
这是我正在使用的列表:
R1 = [["Employee", "Department"],
["Smith", "sales"],
["Black", "production"],
["White", "production"]]
EMPLOYEES = [["Surname", "FirstName", "Age", "Salary"],
["Smith", "Mary", 25, 2000],
["Black", "Lucy", 40, 3000],
["Verdi", "Nico", 36, 4500],
["Smith", "Mark", 40, 3900]]
R2 = [["Department", "Head"],
["production", "Mori"],
["sales", "Brown"]]
对任何格式问题表示歉意,我对 SO 的文本编辑器不太擅长
【问题讨论】:
-
要使用编辑器,您可以直接复制您的代码,然后将其突出显示并单击类似于
{}的图标。这将为编辑器正确缩进您的代码。 -
谢谢,但不是我要问的
-
我意识到我没有回答你的问题(如果我在回答你的问题,我会发布一个答案:-)。我只是想给您留言,希望能帮助您了解如何使用该网站。
-
“is_equal”方法从何而来?
-
能否贴出py.test的输出,包括Assertion Error