簡體   English   中英

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

[英]loops list slicing + elements allocation Python

我是 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 子列表的總和 < 100 for 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]

謝謝!

我不太清楚你認為什么是可接受的 output 所以我假設它是任何元素總和小於 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]。

我添加了一些打印件,以便您可以研究它是如何工作的,但您應該只使用腳本末尾的結果變量來獲取結果。

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)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM