Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/apache-spark/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
程序推理,python函数_Python - Fatal编程技术网

程序推理,python函数

程序推理,python函数,python,Python,程序推理要求我们找到函数的输入,以便它输出特定的结果。 功能是: def func1(stuff, more_stuff=None): data = [] for i, thing in enumerate(stuff): if thing >= 0 or i < 2: data.append(thing + i) print(len(stuff)) if more_stuff is not None:

程序推理要求我们找到函数的输入,以便它输出特定的结果。 功能是:

def func1(stuff, more_stuff=None):
    data = []
    for i, thing in enumerate(stuff):
        if thing >= 0 or i < 2:
            data.append(thing + i)
    print(len(stuff))
    if more_stuff is not None:
        data += more_stuff
    print(data)
我能得到的最接近的是:

4
[5, 3, 2, 5, -3]
以func1([5,2,0,2]、-3])作为输入

我在尝试获取1时遇到困难,我只是想知道如何/为什么可以获取1,就好像它是小于0的值一样,即-1作为该索引的值,那么“thing”是<0,I=2,因此它跳过该值/索引,输出将是:

4
[5, 3, 5, -3]

这里的关键是
stuff
的长度必须是4,而不是第一个for循环之后
数据的长度。如果您试图使用
stuff
填充
数据
,您就遇到了数据[2]永远不能为1的问题,这是绝对正确的

要解决此问题,请在此之前将
stuff
切掉,然后使用
more\u stuff
附加所需的值

Ie您要查找的输入是:

>>>func1([5, 2, -1, -1], [1, 5, -3])
4
[5, 3, 1, 5, -3] 

你能澄清到底是什么问题吗?你做过调试吗?
>>>func1([5, 2, -1, -1], [1, 5, -3])
4
[5, 3, 1, 5, -3]