简单的函数组合 class 不起作用(缺少 2 个必需的位置参数)
Simple function-composition class does not work (missing 2 required positional arguments)
我目前正在研究数学并尝试构建一个简单的函数组合器
https://en.wikipedia.org/wiki/Function_composition
我要告诉程序:
查看两个元组并进行比较:
tuple_1 = (2,4)
tuple_2 = (5,2)
tuple_2[1]
和tuple_1[0]
的值相同吗? (都= 2
)
如果是:
Return tuple(tuple_2[0])
和 tuple_1[1]
5 4
我的功能有效:
list_of_tuples_1 = [
(2,4),
(2,5)
]
list_of_tuples_2 = [
(1,2),
(5,2)
]
def give_function_composition(list_1, list_2):
for tuples_2 in list_2:
for tuples_1 in list_1:
if tuples_2[1] == tuples_1[0]:
return(str(tuples_2[0]) + " " + str(tuples_1[1]))
print(give_function_composition(list_of_tuples_1, list_of_tuples_2))
我的class没有:
class Function_Composition():
def __init__(self, list_1, list_2):
self.list_1 = list_1
self.list_2 = list_2
def give_function_composition(self, list_1, list_2):
for tuples_2 in list_2:
for tuples_1 in list_1:
if tuples_2[1] == tuples_1[0]:
return(str(tuples_2[0]) + " " + str(tuples_1[1]))
my_set_1 = Function_Composition([(3,5),(1,5),(5,4)],
[(5,7),(3,6),(1,8)])
print(my_set_1.give_function_composition())
TypeError: give_function_composition() missing 2 required positional arguments: 'list_1' and 'list_2'
这里有什么问题?我给了他两张单子对不对?
您应该引用传递给构造函数的实例数据,而不是两次传递同一个列表:
class Function_Composition():
def __init__(self, list_1, list_2):
self.list_of_tuples_01 = list_1
self.list_of_tuples_02 = list_2
def give_function_composition(self):
for tuples_2 in self.list_of_tuples_02:
for tuples_1 in self.list_of_tuples_01:
if tuples_2[1] == tuples_1[0]:
return(str(tuples_2[0]) + " " + str(tuples_1[1]))
return "No relation found."
my_set_1 = Function_Composition([(3,5),(1,5),(5,4)],
[(5,7),(3,6),(1,8)])
print(my_set_1.give_function_composition())
这输出:
No relation found.
我目前正在研究数学并尝试构建一个简单的函数组合器
https://en.wikipedia.org/wiki/Function_composition
我要告诉程序:
查看两个元组并进行比较:
tuple_1 = (2,4)
tuple_2 = (5,2)
tuple_2[1]
和tuple_1[0]
的值相同吗? (都= 2
)
如果是:
Return tuple(tuple_2[0])
和 tuple_1[1]
5 4
我的功能有效:
list_of_tuples_1 = [
(2,4),
(2,5)
]
list_of_tuples_2 = [
(1,2),
(5,2)
]
def give_function_composition(list_1, list_2):
for tuples_2 in list_2:
for tuples_1 in list_1:
if tuples_2[1] == tuples_1[0]:
return(str(tuples_2[0]) + " " + str(tuples_1[1]))
print(give_function_composition(list_of_tuples_1, list_of_tuples_2))
我的class没有:
class Function_Composition():
def __init__(self, list_1, list_2):
self.list_1 = list_1
self.list_2 = list_2
def give_function_composition(self, list_1, list_2):
for tuples_2 in list_2:
for tuples_1 in list_1:
if tuples_2[1] == tuples_1[0]:
return(str(tuples_2[0]) + " " + str(tuples_1[1]))
my_set_1 = Function_Composition([(3,5),(1,5),(5,4)],
[(5,7),(3,6),(1,8)])
print(my_set_1.give_function_composition())
TypeError: give_function_composition() missing 2 required positional arguments: 'list_1' and 'list_2'
这里有什么问题?我给了他两张单子对不对?
您应该引用传递给构造函数的实例数据,而不是两次传递同一个列表:
class Function_Composition():
def __init__(self, list_1, list_2):
self.list_of_tuples_01 = list_1
self.list_of_tuples_02 = list_2
def give_function_composition(self):
for tuples_2 in self.list_of_tuples_02:
for tuples_1 in self.list_of_tuples_01:
if tuples_2[1] == tuples_1[0]:
return(str(tuples_2[0]) + " " + str(tuples_1[1]))
return "No relation found."
my_set_1 = Function_Composition([(3,5),(1,5),(5,4)],
[(5,7),(3,6),(1,8)])
print(my_set_1.give_function_composition())
这输出:
No relation found.