尝试,获取第二个函数 - 求和列以在两个列表列表中添加每个索引(over_50k& under_50k)我不明白的是如何从training_list函数获取两个列表并在第二个函数中使用它们 - sum_columns,下面是我的代码。我对这一切都很陌生,所以任何一步一步的反馈都会非常感激。
执行代码时,我得到以下错误, 文件“C:/Python34/Lib/rerun.py”,第12行,在main中 over_50k,under_50k = trainging_list() TypeError:'function'对象不可迭代
def main():
def trainging_list():
over_50k = [[1, 2, 3, 4], [3, 4, 5, 6], [7, 8, 9, 8]]
under_50k = [[1, 4, 6, 8], [3, 7, 4, 5], [6, 9, 4, 5]]
print(over_50k)
return over_50k, under_50k
over_50k, under_50k = trainging_list()
def sum_columns(over_50k, under_50k):
print("a")
over_50k_sum_list = [sum(k) for k in zip(*over_50k)]
under_50k_sum_list = [sum(k) for k in zip(*under_50k)]
sums = over_50k_sum_list + under_50k_sum_list
print(sums)
return sums
main()
答案 0 :(得分:0)
您的方法trainging_list()
返回函数对象本身,不能将其分配给两个变量。您可能想要返回两个列表:
def trainging_list():
over_50k = [[1, 2, 3, 4], [3, 4, 5, 6], [7, 8, 9, 8]]
under_50k = [[1, 4, 6, 8], [3, 7, 4, 5], [6, 9, 4, 5]]
print(over_50k)
return over_50k, under_50k
然后定义下一个函数,该函数从未实际执行,因为您需要使用列表调用它:
over_50k, under_50k = trainging_list()
def sum_columns(over_50k, under_50k):
# your code
sum = sum_columns(over_50k, under_50k)