Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/339.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 需要替换为reversed(),或者可能是另一个索引错误_Python_Python 2.7 - Fatal编程技术网

Python 需要替换为reversed(),或者可能是另一个索引错误

Python 需要替换为reversed(),或者可能是另一个索引错误,python,python-2.7,Python,Python 2.7,这个程序正在经历一个基于web的开发环境,实际上是我试图从中获得我所需要的东西,在这种情况下,我实际上无法导入这些东西。为了满足命名要求,我对函数做了一些修改,但基本上应该是一样的。我目前的问题是reversed()没有实现,所以我必须解决这个问题。我尝试创建一个带有for循环的列表,该循环在函数内部和外部都进行了迭代。我还尝试了当前版本,但周围没有()s。现在我得到一个错误,当我运行这个时,我超出了索引的范围。我的替代方案是什么 def combinations_of_options(opti

这个程序正在经历一个基于web的开发环境,实际上是我试图从中获得我所需要的东西,在这种情况下,我实际上无法导入这些东西。为了满足命名要求,我对函数做了一些修改,但基本上应该是一样的。我目前的问题是reversed()没有实现,所以我必须解决这个问题。我尝试创建一个带有for循环的列表,该循环在函数内部和外部都进行了迭代。我还尝试了当前版本,但周围没有()s。现在我得到一个错误,当我运行这个时,我超出了索引的范围。我的替代方案是什么

def combinations_of_options(options, opt_len):
    ''' yields all combinations of option in specific length
    '''
    pool = tuple(options)
    pool_len = len(pool)
    if opt_len > pool_len:
        return
    indices = range(opt_len)
    yield tuple(pool[index] for index in indices)
    while True:
        for index in (range(opt_len,-1,-1)): #this is the line that uses reversed
            if indices[index] != index + pool_len - opt_len:
                break
        else:
            return
        indices[index] += 1
        for dummy_index in range(index+1, opt_len):
            indices[dummy_index] = indices[dummy_index-1] + 1
        yield tuple(pool[index] for index in indices)

简单的答案是在您的
范围内使用
opt_len-1
。请记住,如果范围向前,这将是发射的最后一个值。谢谢!我以前在解决这个问题的其他尝试中也使用过它!