[英]How to use recursion to find the longest string within multiple lists? (Python)
I'm new here and fairly new to python and I have a question. 我是新来的人,对python来说还很新,我有一个问题。 I had a similar question during my midterm a while back and it has bugged me that I cannot seem to figure it out.
中期我有一个类似的问题,这使我感到困惑,我似乎无法弄清楚。
The overall idea was that I had to find the longest string in a nested list. 总体思路是,我必须在嵌套列表中找到最长的字符串。 So I came up with my own example to try and figure it out but for some reason I just can't.
因此,我想出了自己的示例来尝试解决这个问题,但由于某种原因,我无法做到。 So I was hoping someone could tell me what I did wrong and how I can go about the problem without using the function max but instead with a for loop.
因此,我希望有人可以告诉我我做错了什么以及如何解决该问题而不使用功能max,而是使用for循环。 This is my own example with my code:
这是我自己的代码示例:
typing_test = ['The', ['quick', 'brown'], ['fox', ['jumped'], 'over'], 'the', 'lazy', 'dog']
def longest_string (nested_list: 'nested list of strings') -> int:
'''return the longest string within the nested list'''
maximum_length = 0
for word in nested_list:
try:
if type(word) == str:
maximum_length >= len(word)
maximum_length = len(word)
else:
(longest_string((word)))
except:
print('Error')
return maximum_length
My code returns 3 but the highest should be 6 because of the length of jumped I'm not sure if it's going through each list and checking each strings length. 我的代码返回3,但由于跳转的长度,最高应该为6。我不确定是否要遍历每个列表并检查每个字符串的长度。 In short I don't think it is replacing/updating the longest string.
简而言之,我不认为它正在替换/更新最长的字符串。 So if someone can tell me what I'm doing wrong or how to fix my example I would greatly appreciate it.
因此,如果有人可以告诉我我做错了什么或如何修正示例,我将不胜感激。 And thank you very much in advance.
并非常感谢您。
There's a few problems in your code: 您的代码中存在一些问题:
maximum_length = (longest_string((word)))
) maximum_length = (longest_string((word)))
) This is the modified code of your function (with minimal modifications to make it work): 这是函数的修改后的代码(只需进行最少的修改即可使其起作用):
def longest_string (nested_list):
maximum_length = 0
for word in nested_list:
try:
if type(word) == str:
if maximum_length < len(word):
maximum_length = len(word)
else:
maximum_length = longest_string((word))
except:
print('Error')
return maximum_length
You are going to use the recursion to figure out the longest string in a nested list. 您将使用递归来找出嵌套列表中最长的字符串。
The recursion should be able to have an end condition. 递归应该能够具有结束条件。 For your question, I would like to put "If the parameter is a str".
对于您的问题,我想输入“ If parameter is a str”。
Then, suppose the parameter will always be str or list, and we do not consider other boundary conditions, the code will be like: 然后,假设参数将始终为str或list,并且我们不考虑其他边界条件,则代码将类似于:
def longest_string(p):
if type(p) == str:
return len(p) # Return the length of the str.
return max([longest_string(x) for x in p])
You can use a List Comprehension: 您可以使用列表推导:
def longest(lst):
return max([0]+[i for i in lst if isinstance(i,int)]+
[len(s) for s in lst if isinstance(s,str)]+
[longest(L) for L in lst if isinstance(L,list)])
The [0] is needed for an empty list including at the top level. 空列表(包括顶层)需要[0]。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.