如何确定给定数字是否在数字列表中?
我想制作一个函数,以发现给定的数字是否在数字列表中。如何修复以下代码,以便它适用于由列表组成的列表?
lst = [[1, 2], [3, 4],[5, 6, 7]]
num = 9
def in_list_of_list(lst: list) -> bool:
i = 0
while i < len(lst) and lst[i] != num:
i = i + 1
return i < len(lst)
print(in_list_of_list(lst))
I want to make a function that finds out if a given number is in a list of lists of numbers. How do I fix the following code so that it works for a list consisting of lists?
lst = [[1, 2], [3, 4],[5, 6, 7]]
num = 9
def in_list_of_list(lst: list) -> bool:
i = 0
while i < len(lst) and lst[i] != num:
i = i + 1
return i < len(lst)
print(in_list_of_list(lst))
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可以首先将列表弄平,然后轻松地在平面上检查:
You can flatten the list first, and then check easily in the flat one:
您询问该项目是否在列表的任何元素内部
和快速测试
you ask if the item is inside any of the elements of the list
and a quick test