Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/320.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/15.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_Python 3.x - Fatal编程技术网

Python 如何生成对索引值有约束的排列列表?

Python 如何生成对索引值有约束的排列列表?,python,python-3.x,Python,Python 3.x,我需要创建一个给定列表的置换,这个列表有一个约束条件 given input sequence=(x_0, x_1, x_2,..) and an integer k, the output sequence=(x_perm(0), x_perm(1), x_perm(2),..) with perm(i)< i+k. For example for k=2 that means; x_perm(0) is either x_0 or x_1 x_perm(1) is

我需要创建一个给定列表的置换,这个列表有一个约束条件

given input sequence=(x_0, x_1, x_2,..) and an integer k, the output sequence=(x_perm(0), x_perm(1), x_perm(2),..) with perm(i)< i+k. 

For example for k=2 that means;

   x_perm(0) is either x_0 or x_1

   x_perm(1) is either x_0, x_1 or x_2

Another example for input=[3,4,5] and k=2 is:

output=[4,3,5] is a valid permutation, but [5,4,3] is not since x_perm(0) cannot be x_2.
我已经尝试过这段代码,但随机选择包含重复,因此它不会生成所需的结果


最后,我需要得到所有有效的排列。

我想这就是你感兴趣的。 Python提供了查找排列的直接方法。这些方法存在于itertools软件包中

导入itertools包以在python中实现置换方法。此方法将列表作为输入,并返回元组的对象列表

# import itertools
from itertools import permutations 

# Get all permutations of ['a', 'b', 'c'] 
perm = permutations(['a', 'b', 'c']) 

# Print the obtained permutations 
for i in list(perm): 
    print i 
假设你想得到长度为n的排列(假设n=2),那么你所要做的就是

#change
perm = permutations(['a', 'b', 'c']) 

#to
perm = permutations(['a', 'b', 'c'],2) 

我想你的代码差不多完成了。只需要轻轻一碰。你说随机模块包括重复,所以它不会产生你想要的结果。为什么不使用可能的索引和使用过的索引的列表呢。因此,您可以每次生成可能的索引,并删除使用过的索引

def spec_perm(iterable, k):
    import random
    perm=[0]*len(iterable)
    used_index = [] #Initiate the list of used indexes.
    for i in range (0,len(iterable)):
        #Declare the possible range.
        if i+k < len(iterable):
            index_range = list(range(0, i+k))
        else:
            index_range = list(range(0, len(iterable)))
        #Get rid of the used indexes by using a list comprehension. 
        possible_index = [idx for idx in index_range if idx not in used_index] 
        #Make your random index choice AND assign it to a variable 
        #so that you can append it to used_index list later.
        random_index = random.choice(possible_index) 
        perm[i]=iterable[random_index]
        #Update the used indexes by appending the random index choice to the list.
        used_index.append(random_index) 


    return perm
def spec_perm(iterable,k):
随机输入
perm=[0]*len(可调)
used_index=[]启动已用索引列表。
对于范围(0,len(iterable))中的i:
#声明可能的范围。
如果i+k

希望这有帮助。Kolay gelsin:)

我真的不明白你到底想要什么,但你试过itertools.permutations吗?是的,我试过了,但是我不能在这个设置中包含规则perm(I)index_range=list(range(0,I+k))中i+k不应大于len(iterable)。泰克勒:)@GözdeFiliz是的,我错过了。我已相应地编辑了答案。