循环列表切片+元素分配Python
Posted
技术标签:
【中文标题】循环列表切片+元素分配Python【英文标题】:loops list slicing + elements allocation Python 【发布时间】:2020-08-08 19:02:44 【问题描述】:我是 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,子列表的总和 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]
谢谢!
【问题讨论】:
这能回答你的问题吗? Understanding slice notation 你真正想要的是什么?也许需要代码格式化。 我看到了 sline 符号,但这并不是我所需要的。我是 Python 的新手,我试图在帖子的另一个 cmets 中更详细地解释 【参考方案1】:我不太清楚你认为什么是可接受的输出,所以我假设它是其元素总和小于 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]。
我添加了一些打印件,以便您研究它是如何工作的,但您应该只使用脚本末尾的 results 变量来获取结果。
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)
【讨论】:
感谢您的回答!我需要澄清一些事情。我的想法是将上面示例中列表的所有元素相加为 325。这意味着 325/100 它是 3.25 => 这意味着 4 个子列表。现在在这个子列表中,我想添加每个元素 a,b,c,d 但要让 list1 [a,h,i,j] (80,10,5,4) 最接近的总和以上是关于循环列表切片+元素分配Python的主要内容,如果未能解决你的问题,请参考以下文章