def sudoku_solver(sudoku):
l = [0, 0]
if not find_empty_location(sudoku, l):
return True
row = l[0]
col = l[1]
for num in range(1, 10):
if check_location_is_safe(sudoku, row, col, num):
sudoku[row][col] = num
if sudoku_solver(sudoku):
return True,sudoku
sudoku[row][col] = 0
return False
def P():
a = np.zeros((9,9))
if sudoku_solver(sudoku):
a = sudoku_solver(sudoku)[1]
else:
a = 1
return a
有两个返回值,True
和sudoku
。如何仅在函数P中调用sudoku
?当我运行函数P()
时,它将显示
'bool' object is not subscriptable
。
答案 0 :(得分:0)
看到时
df <- read.table(text =
"ID country side
1 arg 1
1 usa 0
2 ita 1
2 usa 0
2 uk 1
3 aus 0
3 uk 1
4 mx 1
4 uk 0", header = T)
您可以像这样捕获返回的值
return True,sudoku
或者如果您对结果不感兴趣
result, sudoku = my_function()
---编辑---
您的问题似乎围绕着两个收益。即
_, sudoku = my_function()
与
return True,sudoku
这增加了不必要的复杂性。我可以建议您改为使用
来简化吗return False
然后再
return sudoku
这意味着您可以像这样检查返回的值
return None
答案 1 :(得分:0)
您可以使函数返回一个值,该值可以是None或sudoku数据。
Python将None视为错误,因此您的条件可能是:
SELECT id FROM tablename WHERE value = 'hello' OR value = 'world' GROUP BY id
请注意,如果sudoku_solver()是一个复杂且耗时的函数,则您需要在测试条件之前将其结果放入变量中,以使它不会执行两次(与上述条件一样)< / p>
SELECT * FROM (SELECT id,GROUP_CONCAT(VALUE) AS concatvalues FROM testtable GROUP BY id) mytable2 WHERE concatvalues LIKE '%hello%' AND concatvalues LIKE '%world%'
另一种选择是系统地返回带有布尔值和数独数据(或无)的元组。
您的return语句必须更改为...
if sudoku_solver(sudoku):
a = sudoku_solver(sudoku)
...
和...
a = sudoku_solver(sudoku)
if a:
...
那么您的条件可以直接使用索引:
return True,sudoku
但是同样,您可能不想执行两次函数,所以:
return False,None
另一件事是,如果要将数独作为参数传递,则必须意识到即使返回False,函数也会修改调用变量的内容。那可能不是您想要的,但如果是的话,那么就没有必要返回第二个值,因为调用者的sudoku变量实例已经被修改并且可用。
答案 2 :(得分:-1)
您不能从一个函数返回多个对象,但是可以返回一个包含多个成员对象的容器对象,例如列表或元组。尝试使用
<- PUT /api/v1/entrance/login (361ms 200)
| The requesting user agent has been successfully logged in.
|
| Under the covers, this stores the id of the logged-in user in the session as the `userId` key. The next time this user agent sends a request, assuming it includes a cookie (like a web browser), Sails will automatically make this user id available as req.session.userId in the corresponding action. (Also note that, thanks to the included "custom" hook, when a relevant request is received from a logged-in user, that user's entire record from the database will be fetched and exposed as `req.me`.)
|
°
<- GET /api/v1/user/me/overview/subscribe (26ms 200)