Python 使用一般数量的嵌套循环从列表元素创建选择
我试图创建一个函数,该函数将列表作为输入,并返回列表中特定数量元素的所有可能选择,而不使用Python 使用一般数量的嵌套循环从列表元素创建选择,python,python-3.x,list,set,combinations,Python,Python 3.x,List,Set,Combinations,我试图创建一个函数,该函数将列表作为输入,并返回列表中特定数量元素的所有可能选择,而不使用itertools中的内置composition()函数 例如,如果我们将列表[1,2,3,4]和数字3传递给这样一个函数,该函数应该返回[[1,2,3],[1,2,4],[1,3,4],[2,3,4] 如果要选择的元素数为3,我已经创建了此函数的一个版本,但我不知道如何处理嵌套循环和比较的通用数 我的代码: sample_set = [1,2,3,4] def selection
itertools
中的内置composition()
函数
例如,如果我们将列表[1,2,3,4]
和数字3传递给这样一个函数,该函数应该返回[[1,2,3],[1,2,4],[1,3,4],[2,3,4]
如果要选择的元素数为3,我已经创建了此函数的一个版本,但我不知道如何处理嵌套循环和比较的通用数
我的代码:
sample_set = [1,2,3,4]
def selections(input_set, number):
if number == 3:
#frozensets are used here so a set of sets can be generated and so that frozensets containing the same elements in different order can be removed when we apply set()
frozenset_list = [frozenset((i, j, k)) for i in input_set for j in input_set for k in input_set if i != j !=k !=i ]
unique_frozenset_list = set(frozenset_list) #removing identical frozensets
return [list(i) for i in unique_frozenset_list] #converting all frozensets to lists
print(selections(sample_set, 3))
我得到了一个要删除的索引组合,因此如果您想要从5个列表中删除3个项目,那么我可以删除两个索引的组合,例如
(2,5)
我加入了一些条件,以确保没有重复项,并且列表的长度将达到最大值,即nCr,nCr
是一个数学公式
然后,我将只使用这些组合生成不包含这些组合的索引的列表,并添加到主列表final\u list
import random
import math
def go(sample_set,num):
new_list = []
n = len(sample_set)
r = len(sample_set) - num
nCr = (math.factorial(n) / math.factorial(r) / math.factorial(n - r))
while len(new_list) < int(nCr):
co = [random.randint(0,len(sample_set)-1) for count in range(r)]
if len(co) == len(set(co)) and co not in new_list:
new_list.append(co)
final_list = []
for x in new_list:
combination = [q for q in sample_set if sample_set.index(q) not in x]
final_list.append(combination)
return sorted(final_list) # sorted is optional
print(go([1, 2, 3, 4],3))
>>> [[1, 2, 3], [1, 2, 4], [1, 3, 4], [2, 3, 4]]
print(go([1, 2, 3, 4, 5],3))
>>> [[1, 2, 3], [1, 2, 3], [1, 2, 4], [1, 2, 5], [1, 4, 5], [1, 4, 5], [2, 3, 4], [2, 3, 4], [2, 3, 5], [3, 4, 5]]
随机导入
输入数学
def go(样本集,数量):
新列表=[]
n=长度(样本集)
r=长度(样本集)-数量
nCr=(数学阶乘(n)/数学阶乘(r)/数学阶乘(n-r))
而len(新列表)>> [[1, 2, 3], [1, 2, 4], [1, 3, 4], [2, 3, 4]]
打印(go([1,2,3,4,5],3))
>>> [[1, 2, 3], [1, 2, 3], [1, 2, 4], [1, 2, 5], [1, 4, 5], [1, 4, 5], [2, 3, 4], [2, 3, 4], [2, 3, 5], [3, 4, 5]]
我刚刚意识到我可以向前而不是向后做,比如只需要获得3个索引的所有可能组合(或给定的任何数字),然后根据这些组合打印一个列表
def go(sample_set,num):
new_list = []
n = len(sample_set)
nCr = (math.factorial(n) / math.factorial(num) / math.factorial(n - num))
while len(new_list) < int(nCr):
co = [random.randint(0,len(sample_set)-1) for count in range(num)]
if len(co) == len(set(co)) and co not in new_list:
new_list.append(co)
final_list = []
for x in new_list:
combination = [q for q in sample_set if sample_set.index(q) in x]
final_list.append(combination)
return sorted(final_list) # sorted is optional
print(go([1, 2, 3, 4],3))
def go(样本集,num):
新列表=[]
n=长度(样本集)
nCr=(math.factorial(n)/math.factorial(num)/math.factorial(n-num))
而len(新列表)
导入itertools;out=list(itertools.combinations(sample_set,r=3))
@thierry对不起,我忘了提一下,我不想使用内置函数,我想自己创建它。你还需要这个吗answered@coderoftheday是的,这个代码可以缩短,但这取决于给定的列表是否有重复项?所以,我们基本上从给定列表随机生成给定长度的列表,直到生成的唯一选择数为nCr。有没有办法找到随机函数在给定情况下生成所有这些可能性所需的平均时间?也许我可以不使用随机函数,所以使用for循环以每一个其他数字获得每个数字,因此所用的时间将与列表的长度成正比,我回家后会试试这个,但是对于一个随机列表,不会有平均值,因为它是random
我不使用random就无法计算出来