使用一般数量的嵌套循环从列表的元素创建选择 [英] creating selections from the elements of a list using a general number of nested loops
问题描述
我试图创建一个将列表作为输入并从列表中返回特定数量元素的所有可能选择的函数,而不使用 itertools中的内置
. combination()
函数
I am trying to create a function that takes a list as input and returns all possible selections of a particular number of elements from a list without using the inbuilt combination()
function in itertools
.
例如,如果我们将列表[1,2,3,4]
和数字3传递给此类函数,则该函数应返回 [[1,2,3],[1,2,4],[1,3,4],[2,3,4]]
For example, if we pass the list [1,2,3,4]
and the number 3 to such a function, the function should return [[1,2,3],[1,2,4],[1,3,4],[2,3,4]]
如果要选择的元素数为3,我已经创建了此函数的版本,但是我不知道如何处理用于循环和比较的嵌套嵌套数.
I have created a version of this function if the number of elements to be selected is 3 but I do not know how to deal with a generic number of nested for loops and comparisons.
我的代码:
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)
I got a combination of indexes to remove, so if you wanted 3 items from a list of 5, there would be combinations of two indexes I could remove e.g (2,5)
在某些情况下,我确保没有重复,并且列表的长度最大为nCr, nCr
是一个数学公式.
I threw in some conditions to make sure there were no duplicates, and that the length of list would be to the max which would be nCr, nCr
is a maths formulae.
然后,我将使用这些组合创建不包含这些组合索引的列表,并将其添加到主列表中 final_list
Then I would just use these combinations to make list that didn't include the indexes from these combinations, and added to the master list final_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]]
我刚刚意识到我可以向前而不是向后进行操作,因为只需获取3个索引(或给出的任何数字)的所有可能组合,然后基于该索引打印一个列表即可.
I just realised I can do it forwards instead of backwards, as in just get all possible combinations of 3 indexes (or whatever number is given) and print a list based on that.
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))
这篇关于使用一般数量的嵌套循环从列表的元素创建选择的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!