简体   繁体   English

如何在为每个列表选择单个元素时从列表列表中查找 n 个元素的组合

[英]How to find combination of n elements from list of lists while choosing single element for each list

Looking for combinations for n elements from list of lists but choosing only one element from each list.从列表列表中寻找 n 个元素的组合,但从每个列表中仅选择一个元素。 Eg例如

list =  [[a, b], [c,d], [e,f,g,h,i], [j,k,l,m, n], [o,p]..]

While choosing no more than one element from each list, I am trying to come up with different combinations虽然从每个列表中选择不超过一个元素,但我试图提出不同的组合

eg: for combination of n = 2 elements:例如:对于n = 2元素的组合:

[a,c] [b,c], [c,j]...so on

for combination of n = 3 elements:对于n = 3元素的组合:

[a,c,e], [a,d,f]..so on

for combination of n = 4 elements:对于n = 4元素的组合:

[a, c, j, o], [a,c, i, p] ...

I tried using itertools combinations, but quickly running into issues.我尝试使用 itertools 组合,但很快就遇到了问题。 Any hints?有什么提示吗?

Is this what you are trying to achieve?这是你想要达到的目标吗?

import itertools
from pprint import pprint

original_lst = [['a', 'b'], ['c','d'], ['e','f','g','h','i'], ['j','k','l','m','n'], ['o','p']]

def two_on_same_sublist(items, lst):
    for sub in lst:
        for i in itertools.combinations(items, 2):
            if all(x in sub for x in i):
                return True
            else:
                continue


def possible_combinations_of_items_in_sub_lists_no_rep(lst, n):
    flat_lst = [i for sublist in lst for i in sublist] # Make it one flat list
    return [i for i in itertools.combinations(flat_lst, n) if not two_on_same_sublist(i, lst)]

pprint(possible_combinations_of_items_in_sub_lists_no_rep(original_lst, 3))

I think you are looking for something like this.我想你正在寻找这样的东西。

Find the min and max size of sub list.查找子列表的最小和最大大小。 Then create a dictionary with keys ranging from min to max.然后创建一个字典,其键范围从 min 到 max。

Code:代码:

from collections import defaultdict

c = defaultdict(list)

lst =  [['a','b'],
        ['c','d'],
        ['e','f','g','h','i'],
        ['j','k','l','m','n'],
        ['o','p'],
        ['q','r','s'],
        ['t','u','v'],
        ['w','x','y','z']]

a = min(map(len,lst)) #find min of sub list - here it will be 2
b = max(map(len,lst)) #find max of sub list - here it will be 5

for v in lst: #iterate thru the full list
    c[len(v)].append(v) #store values into a dictionary with key = len (sublist)

c = dict(c) #convert it back to normal dict

#iterate from min thru max and print the combinations
#skip if key not found in dictionary

for i in range (a, b+1):
    if i in c: print ('Combinations of n =',i, 'elements are :', c[i])

Output: Output:

Combinations of n = 2 elements are : [['a', 'b'], ['c', 'd'], ['o', 'p']]
Combinations of n = 3 elements are : [['q', 'r', 's'], ['t', 'u', 'v']]
Combinations of n = 4 elements are : [['w', 'x', 'y', 'z']]
Combinations of n = 5 elements are : [['e', 'f', 'g', 'h', 'i'], ['j', 'k', 'l', 'm', 'n']]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 从列表列表中,如何找到具有 n 个共同元素的列表 - From a list of lists, how do I find lists with n elements in common 如何将列表的字典列表转换为每个组合的字典列表 - How to turn a list of dictionaries of lists into a list of lists of dictionaries for each combination 如何使用特定元素在python中打印列表列表的每个元素 - How to print each element of a list of lists in python using the specific elements 如何从具有嵌套列表作为元素的列表中选择 n 个最小的数字 - How to pick n smallest numbers from a list with nested lists as elements 从列表列表中识别唯一值,每个列表至少有 n 个唯一元素 - Identify unique values from list of lists, with each list having at least n unique elements 从列表列表中获取前 n 个元素 - Get the n top elements from a list of lists 在列表python中查找元素左侧的n个元素 - find n elements on the left of an element in list python Python:如何在 n 个长列表中找到三个元素的所有可能组合 - Python: How to find every possible combination of three elements taken together in n long list 使用 Itertools 创建来自多个列表的元素组合列表 - Using Itertools to create a list of combination of elements from multiple lists 从最大分散重复元素的列表列表中找到最佳组合 - Finding the optimal combination from a list of lists that maximally disperses repeating elements
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM