This question already has answers here:
Handle either a list or single integer as an argument
                                
                                    (5个答案)
                                
                        
                                4年前关闭。
            
                    
能够将列表或值传递给函数并将其返回为的最佳方法是什么

foo(x)foo(x)

foo([x, y, z])[foo(x), foo(y), foo(z)]

只是测试输入是否为列表?一个try / except块试图访问列表元素?还有吗

最佳答案

使用集合来确定它是否可迭代。如果可迭代,请执行列表逻辑。如果不这样做,您的可变逻辑

import collections

if isinstance(e, collections.Iterable) and not isinstance(e, basestring) :


正如评论中指出的那样。添加逻辑以不包含字符串

关于python - 将列表或值传递给Python中的函数,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/33675987/

10-12 00:48
查看更多