首页 > 解决方案 > “TypeError:'NoneType'对象不可迭代”在python

问题描述

我的代码:我从网站上获取并编辑了一些以获取列表作为输出

def anit_printArr(a, n): 
    b=[]
    for i in range(n):
        b.append(a[i])
    #yield b #I tried this, but then it shows error: TypeError: 'NoneType' object is not iterable
    print(b) 

# Generating permutation using Heap Algorithm 
def anit_heapPermutation(a, size, n): 

    # if size becomes 1 then prints the obtained 
    # permutation 
    if (size == 1): 
        anit_printArr(a, n) 
        return

    for i in range(size): 
        anit_heapPermutation(a,size-1,n); 

        # if size is odd, swap first and last 
        # element 
        # else If size is even, swap ith and last element 
        if size&1: 
            a[0], a[size-1] = a[size-1],a[0] 
        else: 
            a[i], a[size-1] = a[size-1],a[i] 


# Driver code 
a = [1, 2, 3, 4] 
n = len(a) 
anit_heapPermutation(a, n, 3) #This works fine, I tried to yield but...
for i in anit_heapPermutation(a, n, 3): #I want to use like this
    print(i) #This gives: TypeError: 'NoneType' object is not iterable

我想在 for 循环中迭代这个函数,但出现错误。是的,我是初学者。

标签: pythonloopsiteratortypeerroriterable

解决方案


推荐阅读