循环列表切片+元素分配Python

问题描述 投票:1回答:1

我是Python的初学者,正在尝试执行以下操作:

main_list[80,80,30,30,30,30,20,10,5,4,3,2,1] #list of integers-将main_list切片为多个列表,例如list1,2,3,..,n,子列表之和<100for i in range of n: print(list(i))list1 [80,20],list2 [80,10,5,4,1],list3 [30,30,30],listn [30,3,2]

谢谢!

python loops linked-list slice
1个回答
0
投票

我不太清楚您认为可接受的输出是什么,所以我假设它是元素总数小于100的任何列表。

我发现的解决方案是使用递归。对于列表[a,b,c,d],我们将检查此子列表的条件:

[a]
[a, b] (if the condition for [a] is met)
[a, b, c] (if the condition for [a, b] is met)
[a, b, c, d] (if the condition for [a, b, c] is met)
[a, c] (if the condition for [a] is met)
[a, c, d] (if the condition for [a, c] is met)
[a, d] (if the condition for [a] is met)

[b]
[b, c] (if the condition for [b] is met)
[b, c, d] (if the condition for [b, c] is met)
[b, d] (if the condition for [b] is met)

[c]
[c, d] (if the condition for [c] is met)

[d]

概念是,对于列表中的“ n”元素,我们将寻找满足要求的大小为“ n-1”到0(即元素本身)的子列表。子列表由每个迭代的被研究元素右侧的元素形成,因此对于前30个,要使用的子列表为[30、30、30、20、10、5、4、3、2、1 ]

此查找每个元素的子列表的过程是使用递归的过程。它为子列表的每个元素调用自身,以检查是否满足条件。对于上面的示例,如果满足[a,b]的条件,则它还将尝试[a,b,c]和[a,b,d](通过将自身与(a,b)求和和子列表[c,d]。

我添加了一些打印件,因此您可以研究其工作原理,但是您应该只在脚本末尾使用result变量来获取结果。

main_list = [80,80,30,30,30,30,20,10,5,4,3,2,1] 

def less_than_hundred(input) -> bool:
    return input < 100

def sublists_meet_condition(condition, input):
    """
    This function is used to call the sublists_for_element function with every element in the original list and its sublist:
    - For the first element (80) it calls the second function with the sublist [80,30,30,30,30,20,10,5,4,3,2,1]
    - For the fifth element (30) it calls the second function with the sublist [30,20,10,5,4,3,2,1]
    Its purpose is to collect all the sublists that meet the requirements for each element
    """
    results = []
    for index, element in enumerate(input):
        print('Iteration {} - Element {}'.format(index, element))
        if condition(element):
            results.append([element])
            print('{} = {}'.format([element], element))
            num_elements = len(input) - index
            main_element = element
            sublist = input[index+1:]
            for result in sublists_for_element(condition, main_element, sublist):
                new_result = [element] + result
                sum_new_result = sum(new_result)
                results.append(new_result)
                print('{} = {}'.format([element] + result, sum_new_result))
    return results

def sublists_for_element(condition, sum_main_elements, sublist):
    """
    This function is used to check every sublist with the given condition.
    The variable sum_main_elements allows the function to call itself and check if for a given list of numbers that meet the conditions [30, 30, 4] for example, any of the elements of the remaining sublists also meets the condition for example adding the number 3 still meets the condition.
    Its purpose is to return all the sublists that meet the requirements for the given sum of main elements and remaining sublist
    """
    num_elements = '{}{}'.format('0' if len(sublist) + 1 < 10 else '',len(sublist) + 1)
    #print('Elements: {} | Main element: {} | Sublist: {}'.format(num_elements, sum_main_elements, sublist))
    result = []
    for index, element in enumerate(sublist):
        if condition(sum_main_elements + element):
            result.append([element])
            sublist_results = sublists_for_element(condition, sum_main_elements + element, sublist[index+1:])
            for sublist_result in sublist_results:
                result.append([element] + sublist_result)
    return result

results = sublists_meet_condition(less_than_hundred, main_list)
© www.soinside.com 2019 - 2024. All rights reserved.