Python 重构递归;";功能 def递归计数(目标、嵌套计数列表): #此代码在“嵌套的\u num\u列表”中查找所有出现的“target” #重写此代码,而不使用while/for循环来实现 #同样的结果。基本上只使用递归调用和if。 计数=0 i=0 而i
这是我在计算课上提出的一个额外问题(阅读标签)。我尝试了默认参数,修补了I和数数的方法,但我无法得到它。你们这些可爱的人会怎么做Python 重构递归;";功能 def递归计数(目标、嵌套计数列表): #此代码在“嵌套的\u num\u列表”中查找所有出现的“target” #重写此代码,而不使用while/for循环来实现 #同样的结果。基本上只使用递归调用和if。 计数=0 i=0 而i,python,recursion,Python,Recursion,这是我在计算课上提出的一个额外问题(阅读标签)。我尝试了默认参数,修补了I和数数的方法,但我无法得到它。你们这些可爱的人会怎么做 def recursive_count(target, nested_num_list): # This code finds all occurrences of "target" in "nested_num_list" # Rewrite this code without a while/for loop that achieves #
def recursive_count(target, nested_num_list):
# This code finds all occurrences of "target" in "nested_num_list"
# Rewrite this code without a while/for loop that achieves
# the same results. Basically using only recursive calls and if's.
count = 0
i = 0
while i < len(nested_num_list):
if nested_num_list[i] == target:
count += 1
if type(nested_num_list[i]) == type([]):
count += recursive_count(target, nested_num_list[i])
i += 1
return count
如果您不介意修改输入参数,您可以每次从列表中弹出第一项并将其传递回。
编辑:添加了嵌套处理。我将编写一个递归展平器并使用其输出
def recursive_count(target, nested_num_list):
count = 0
# Make sure there's still stuff left.
if len(nested_num_list) is 0:
return 0
item = nested_num_list.pop(0)
if type(item) == type([]):
count += recursive_count(target, item)
elif target == item:
count += 1
count += recursive_count(target, nested_num_list)
return count
编辑:所以您需要一个函数来执行此操作,这里有一个版本没有isinstance
或显式长度检查或索引,只有一个内联-(如果
):
def flattener(left, right):
try:
res = reduce(flattener, right, left)
except TypeError:
left.append(right)
res = left
return res
>>> nested_list = [[0], [1, [2, 3]], [4, 5], [6, [7], [8, 9]], 10, [[[[11]]]], [], 12]
>>> flattened_list = reduce(flattener, nested_list, [])
>>> flattened_list
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]
Go on with flattened_list ...
这里是Python3的另一种方法(很容易翻译成Python2)。不修改输入参数或使用其他功能(除<代码>isinstance):
提示:将列表分为第一个元素和其余元素。所有操作都必须在func中完成。没有辅助工具:)但
展开式优于嵌套式!;)recursive_count
接受2个参数,并且不会下降到嵌套列表中。您的代码返回的次数较少,因此它应该给出一个嵌套列表[1,2,3、[1,1,1],[1]]。对于目标“1”,当正确答案为5时,代码将导致1次出现。不,你他妈的可以修改他们!哈哈当!抱歉,忘了它是在一个测试的循环上。
def flattener(left, right):
try:
res = reduce(flattener, right, left)
except TypeError:
left.append(right)
res = left
return res
>>> nested_list = [[0], [1, [2, 3]], [4, 5], [6, [7], [8, 9]], 10, [[[[11]]]], [], 12]
>>> flattened_list = reduce(flattener, nested_list, [])
>>> flattened_list
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]
Go on with flattened_list ...
def countin(seq, predicate):
try:
iterseq = iter(seq)
except TypeError:
return 1 if predicate(seq) else 0
try:
head = iterseq.next()
except StopIteration:
return 0
c = countin(head, predicate)
c += countin(iterseq, predicate)
return c
>>> count_in(nested_list, lambda x: x % 2 == 0) # count all even numbers
7
>>> len(filter(lambda x: x % 2 == 0, reduce(flattener, nested_list, [])))
7
def recursive_count(target, nested_num_list):
if nested_num_list == []:
return 0
if isinstance(nested_num_list, int):
return nested_num_list == target
x, *y = nested_num_list
# x, y = nested_num_list[0], nested_num_list[1:] # Python 2
return recursive_count(target, x) + recursive_count(target, y)
>>> recursive_count(1, [1,2,3,[1,1,1],[1]])
5