用可迭代的东西替换函数(Replacing a function with something iterable)

简而言之。 我怎么写别的东西: for another in combinationOfK(K-1, L[i+1:]):我的函数combinationOfK(...)是不可迭代的。

我试图从这里理解代码,解决方案。 Problem 26: Generate the combinations of K distinct objects chosen from the N elements of a list 我知道收益率是多少。 但我试图在没有 yield语句的情况下编写代码带有yield语句的代码就是这个。

def combination(K, L): if K<=0: yield [] return for i in range(len(L)): thisone = L[i:i+1] for another in combination(K-1, L[i+1:]): yield thisone + another

yield-keyword-explained这个问题让我觉得我可以替代产量。 他们给予的接受,对我来说不起作用,是:

当你看到一个带有yield语句的函数时,应用这个简单的技巧来理解会发生什么:

在函数的开头插入行result = [] 。 用result.append(expr)替换每个yield expr 。 在函数底部插入一行return result 。 耶 - 没有更多的yield声明! 阅读并找出代码。 将功能恢复为原始定义。

使用它来获得没有收益的代码给我这个。 代码不起作用(该函数不可迭代)。 我需要编写什么才能使这些代码无损地工作?

def combinationOfK(K,L): result = [] if K <= 0: result.append([]) return for i in range(len(L)): thisone = L[i:i+1] for another in combinationOfK(K-1, L[i+1:]): # the error result.append(thisone + another) return result

我正在使用此代码来测试函数,

the_list = ['a','b','c','d','e'] print list(combinationOfK(2, the_list))

引发错误TypeError: 'NoneType' object is not iterable 。

In short. How do I write something else than this: for another in combinationOfK(K-1, L[i+1:]): My function combinationOfK(...) is not iterable.

I am trying to understand the code from here, solution to. Problem 26: Generate the combinations of K distinct objects chosen from the N elements of a listI know what yield does. But I am trying to write the code without a yield statement. Code with yield statement is this.

def combination(K, L): if K<=0: yield [] return for i in range(len(L)): thisone = L[i:i+1] for another in combination(K-1, L[i+1:]): yield thisone + another

The question, yield-keyword-explained gave me the idea that I could replace yield. The recepie they give, which is not working for me, is:

When you see a function with yield statements, apply this easy trick to understand what will happen:

Insert a line result = [] at the start of the function. Replace each yield expr with result.append(expr). Insert a line return result at the bottom of the function. Yay - no more yield statements! Read and figure out code. Revert function to original definition.

Using this to get code without yield give me this. The code is not working (the function is not iterable). What do I have to write to get this code working without yield?

def combinationOfK(K,L): result = [] if K <= 0: result.append([]) return for i in range(len(L)): thisone = L[i:i+1] for another in combinationOfK(K-1, L[i+1:]): # the error result.append(thisone + another) return result

I am using this code to test the function,

the_list = ['a','b','c','d','e'] print list(combinationOfK(2, the_list))

raising error TypeError: 'NoneType' object is not iterable.

最满意答案

问题是您的原始代码以不寻常的方式使用return 。

def combination(K, L): if K<=0: yield [] return # <--- hmmm

大多数时候你不会在发电机中看到return ,因为你并不经常需要它。 通常情况下,发电机最终会“脱落”; 解释器到达生成器的末尾而不遇到return语句,然后它知道抛出StopIteration 。

这种情况下,代码的编写者插入了一个return语句来“快点”进程。 当K <= 0 ,没有更多工作要做,因此生成器可以抛出StopIteration - 但是如果没有return语句,它将进入for循环,产生不正确的结果。 在我看来,更清楚的方法就是这样:

def combination(K, L): if K<=0: yield [] else: for i in range(len(L)): thisone = L[i:i+1] for another in combination(K-1, L[i+1:]): yield thisone + another

现在转换按预期工作:

def combination2(K, L): result = [] if K <= 0: result.append([]) else: for i in range(len(L)): thisone = L[i:i + 1] for another in combination2(K - 1, L[i + 1:]): result.append(thisone + another) return result

The problem is that your original code uses return in an unusual way.

def combination(K, L): if K<=0: yield [] return # <--- hmmm

Most of the time you won't see return in a generator, because you don't often need it. Usually, generators simply "fall off" at the end; the interpreter reaches the end of the generator without encountering a return statement, and then it knows to throw StopIteration.

In this case, the writer of the code has inserted a return statement to "hurry up" the process. When K <= 0, there's no more work to be done, so the generator can throw StopIteration -- but without the return statement, it would go into the for loop, producing incorrect results. In my opinion, a clearer way to do this would have been like so:

def combination(K, L): if K<=0: yield [] else: for i in range(len(L)): thisone = L[i:i+1] for another in combination(K-1, L[i+1:]): yield thisone + another

Now the conversion works as expected:

def combination2(K, L): result = [] if K <= 0: result.append([]) else: for i in range(len(L)): thisone = L[i:i + 1] for another in combination2(K - 1, L[i + 1:]): result.append(thisone + another) return result用可迭代的东西替换函数(Replacing a function with something iterable)

简而言之。 我怎么写别的东西: for another in combinationOfK(K-1, L[i+1:]):我的函数combinationOfK(...)是不可迭代的。

我试图从这里理解代码,解决方案。 Problem 26: Generate the combinations of K distinct objects chosen from the N elements of a list 我知道收益率是多少。 但我试图在没有 yield语句的情况下编写代码带有yield语句的代码就是这个。

def combination(K, L): if K<=0: yield [] return for i in range(len(L)): thisone = L[i:i+1] for another in combination(K-1, L[i+1:]): yield thisone + another

yield-keyword-explained这个问题让我觉得我可以替代产量。 他们给予的接受,对我来说不起作用,是:

当你看到一个带有yield语句的函数时,应用这个简单的技巧来理解会发生什么:

在函数的开头插入行result = [] 。 用result.append(expr)替换每个yield expr 。 在函数底部插入一行return result 。 耶 - 没有更多的yield声明! 阅读并找出代码。 将功能恢复为原始定义。

使用它来获得没有收益的代码给我这个。 代码不起作用(该函数不可迭代)。 我需要编写什么才能使这些代码无损地工作?

def combinationOfK(K,L): result = [] if K <= 0: result.append([]) return for i in range(len(L)): thisone = L[i:i+1] for another in combinationOfK(K-1, L[i+1:]): # the error result.append(thisone + another) return result

我正在使用此代码来测试函数,

the_list = ['a','b','c','d','e'] print list(combinationOfK(2, the_list))

引发错误TypeError: 'NoneType' object is not iterable 。

In short. How do I write something else than this: for another in combinationOfK(K-1, L[i+1:]): My function combinationOfK(...) is not iterable.

I am trying to understand the code from here, solution to. Problem 26: Generate the combinations of K distinct objects chosen from the N elements of a listI know what yield does. But I am trying to write the code without a yield statement. Code with yield statement is this.

def combination(K, L): if K<=0: yield [] return for i in range(len(L)): thisone = L[i:i+1] for another in combination(K-1, L[i+1:]): yield thisone + another

The question, yield-keyword-explained gave me the idea that I could replace yield. The recepie they give, which is not working for me, is:

When you see a function with yield statements, apply this easy trick to understand what will happen:

Insert a line result = [] at the start of the function. Replace each yield expr with result.append(expr). Insert a line return result at the bottom of the function. Yay - no more yield statements! Read and figure out code. Revert function to original definition.

Using this to get code without yield give me this. The code is not working (the function is not iterable). What do I have to write to get this code working without yield?

def combinationOfK(K,L): result = [] if K <= 0: result.append([]) return for i in range(len(L)): thisone = L[i:i+1] for another in combinationOfK(K-1, L[i+1:]): # the error result.append(thisone + another) return result

I am using this code to test the function,

the_list = ['a','b','c','d','e'] print list(combinationOfK(2, the_list))

raising error TypeError: 'NoneType' object is not iterable.

最满意答案

问题是您的原始代码以不寻常的方式使用return 。

def combination(K, L): if K<=0: yield [] return # <--- hmmm

大多数时候你不会在发电机中看到return ,因为你并不经常需要它。 通常情况下,发电机最终会“脱落”; 解释器到达生成器的末尾而不遇到return语句,然后它知道抛出StopIteration 。

这种情况下,代码的编写者插入了一个return语句来“快点”进程。 当K <= 0 ,没有更多工作要做,因此生成器可以抛出StopIteration - 但是如果没有return语句,它将进入for循环,产生不正确的结果。 在我看来,更清楚的方法就是这样:

def combination(K, L): if K<=0: yield [] else: for i in range(len(L)): thisone = L[i:i+1] for another in combination(K-1, L[i+1:]): yield thisone + another

现在转换按预期工作:

def combination2(K, L): result = [] if K <= 0: result.append([]) else: for i in range(len(L)): thisone = L[i:i + 1] for another in combination2(K - 1, L[i + 1:]): result.append(thisone + another) return result

The problem is that your original code uses return in an unusual way.

def combination(K, L): if K<=0: yield [] return # <--- hmmm

Most of the time you won't see return in a generator, because you don't often need it. Usually, generators simply "fall off" at the end; the interpreter reaches the end of the generator without encountering a return statement, and then it knows to throw StopIteration.

In this case, the writer of the code has inserted a return statement to "hurry up" the process. When K <= 0, there's no more work to be done, so the generator can throw StopIteration -- but without the return statement, it would go into the for loop, producing incorrect results. In my opinion, a clearer way to do this would have been like so:

def combination(K, L): if K<=0: yield [] else: for i in range(len(L)): thisone = L[i:i+1] for another in combination(K-1, L[i+1:]): yield thisone + another

Now the conversion works as expected:

def combination2(K, L): result = [] if K <= 0: result.append([]) else: for i in range(len(L)): thisone = L[i:i + 1] for another in combination2(K - 1, L[i + 1:]): result.append(thisone + another) return result