简体   繁体   English

在python中打印数组的子集

[英]printing subsets of an array in python

I am trying to print subsets of an array in python but i am get None type is not iterable error我正在尝试在 python 中打印数组的子集,但我得到 None 类型不是可迭代错误

def printSubs(l,out):
  if (len(l) == 0):
    print(*out)
    return

  printSubs(l[1:],out)
  printSubs(l[1:],out.append(l[0]))
printSubs([15,20,12],[])

this throws None type is not iterable error but when i copy the out into a new array and then append l[0] to it and pass it to the second function call it works fine这会抛出 None 类型不是可迭代错误,但是当我将输出复制到一个新数组中,然后将 l[0] 附加到它并将其传递给第二个函数调用时,它工作正常

def printSubs(l,out):
  if (len(l) == 0):
    print(*out)
    return

  printSubs(l[1:],out)
  newOut = [i for i in out]
  newOut.append(l[0])

  printSubs(l[1:],newOut)
printSubs([15,20,12],[])

this code works fine can anyone explain me why is it happening to me it seems both code does the same thing as the out array is having different states in diffrent function calls.这段代码工作正常谁能解释我为什么它发生在我身上似乎两个代码都做同样的事情,因为 out 数组在不同的函数调用中具有不同的状态。 Thanks in advance :)提前致谢 :)

out.append(l[0]) doesn't return anything and that's what you're passing in function. out.append(l[0])不返回任何东西,这就是你传入的函数。 That's why this error.这就是这个错误的原因。

Instead of -代替 -

  printSubs(l[1:],out)
  printSubs(l[1:],out.append(l[0]))

You have to do -你必须要做 -

  printSubs(l[1:],out)
  out.append(l[0])
  printSubs(l[1:],out)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM