- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有包含子列表的列表。从这些列表中,我想检索所有按升序排列的整数组合。子列表的顺序也很重要(请参阅预期输出)。
当函数本身也返回整数时,这并不是一件坏事(请参阅预期输出中的可选子列表)。
此外,当子列表具有多个值时,我也想将它们视为单独的组合。这些值不能同时出现(参见示例 3)。
example_list = [[1], [0], [4], [2]]
get_ascending_sublist_values(example_list)
>> [[1, 4], [1, 2], [0, 4], [0, 2] (optional: [1], [0], [4], [2])]
example_list2 = [[1], [0], [4], [2], [5]]
get_ascending_sublist_values(example_list2)
>> [[1, 4, 5], [1, 2, 5], [0, 4, 5], [0, 2, 5], [1, 4], [1, 2], [0, 4], [0, 2], [0, 5], [(optional: [1], [0], [4], [2], [5])]
example_list3 = [[0], [1, 4], [2]]
get_ascending_sublist_values(example_list3)
>> [[0, 1, 2], [0, 1], [0, 4], [0, 2], [1, 2], (optional: [1], [0], [4], [2])]
最佳答案
使用 itertools.combinations
和 itertools.product
.这不是一个有效的解决方案,因为这不是必需的。使这更有效(即使用回溯)需要相当多的工作,而且理论上它仍然不能低于 o(2^n)
.
from itertools import combinations
from itertools import product
def get_ascending_sublist_values(a):
filtered = set()
for comb_length in range(2, len(a)+1):
combs = combinations(a, comb_length)
results = []
for comb in combs:
for i in range(len(comb) - 1):
prods = product(*comb)
for prod in prods:
if sorted(prod) == list(prod):
results.append(tuple(sorted(prod)))
for r in results:
filtered.add(r)
print(filtered)
a1 = [[1], [0], [4], [2]]
a2 = [[1], [0], [4], [2], [5]]
a3 = [[0], [1, 4], [2]]
get_ascending_sublist_values(a1)
print("----------")
get_ascending_sublist_values(a2)
print("----------")
get_ascending_sublist_values(a3)
出去:
{(1, 2), (0, 2), (1, 4), (0, 4)}
----------
{(1, 2), (0, 4, 5), (4, 5), (1, 4), (1, 4, 5), (1, 5), (0, 5), (0, 2, 5), (0, 4), (2, 5), (1, 2, 5), (0, 2)}
----------
{(0, 1), (1, 2), (0, 1, 2), (0, 4), (0, 2)}
关于python - 从子列表中检索升序整数的所有可能组合,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67920773/
我正在做作业,经过几天的努力,我无法弄清楚为什么在实现归并排序后,我的列表仅包含链接列表中的最后一个对象。它不输出我的整个链表,只输出最后一个对象。如何更改代码以阻止列表在一个对象之后变为 null。
我想对一列进行排序(它是一个带有 Y/N 的标志列)。它应该在每次点击时在升序/降序之间切换。 我的代码不起作用..我是 VBA 新手。请提供任何帮助。 Private Sub CommandButt
我对如何让它正常工作有点困惑。我需要从用户那里获取数字(直到他们输入负数或达到最大大小),并且对于他们添加的每个数字,将其按升序插入到正确的索引中。现在,由于某种原因,即使我定义了常量 10,我的数组
我相当困惑如何创建一个按钮,将打印到 php 文件的表中的数据按升序或降序排序。 "> Order by Week Sort Week 这是我想要实现的一个简单示例,我只是停留在 php
我在使用 C++ 中的 priority_queue 时遇到问题,我有一个优先级队列 vector ,优先级队列包含多个 Person 对象。现在,我希望 priority_queue 根据年龄对 P
我正在使用 Lodash 按列对表中的数据进行排序。当我单击表格列标题中的箭头时,该特定表格列将按升序或降序排序。但是,我希望每一列首先按升序排序,而不管其他列的当前顺序如何。现在,我的函数只根据当前
如果事先知道哪些列可用,则以下代码可以重新排列列,但如果想按降序/升序重新排列列怎么办? StackOverflow 上有一些类似的帖子,但没有一篇可以在事先不知道哪些列可用的情况下这样做。 ty
在 woocommerce 中,我使用以下代码添加了自定义费用: add_action( 'woocommerce_cart_calculate_fees', 'custom_fee_based_on
这可以很好地以最多 1000 个项目的步长对数据进行分页: var q1 = (from book in table.CreateQuery() where book.PartitionKe
您好,我正在使用以下内容对表适配器返回的数据表的结果进行排序 Dim spots = myDataTable.Where(Function(t) t.UserID = 1).OrderByDesce
这可以很好地以最多 1000 个项目的步长对数据进行分页: var q1 = (from book in table.CreateQuery() where book.PartitionKe
我正在尝试获取数据库中最近的 n 个条目的列表,但将它们按升序排序。 显然我可以使用以下方法获取前 n 个条目: SELECT owner_id,message FROM messages WHERE
我尝试使用此方法将数据提取到 mysql 表 $query=$conn->query("SELECT * FROM users ORDER BY id_user ASC"); 这是我的表结构 用户 i
我正在使用 NSFetchedResultsController 在列表中显示对象 Event。 Event 对象具有 startDate 属性和 eventType 属性,它是 CheckIn 类型
我有以下代码/数据: import numpy as np data = np.array([ [12, 1, 0.7, 0], [13, 2, 0.5, 1], [41, 3
所以我是 C++ 的新手,我正在尝试一些初学者练习,这是问题所在:我必须按升序和降序对整数数组进行排序,但每次我尝试按升序排序时,都会出现 0在我的数组中无处替换以前的数组整数。只有当我使用“升序”选
在我的应用程序中,我有一个任务列表(不,它不仅仅是另一个待办事项应用程序),我使用 NSFetchedResultsController 在 UITableView 中显示任务。这是相关的初始化代码:
本人由于项目开发中需要对查询结果list进行排序,这里根据的是每一个对象中的创建时间降序排序。本人讲解不深,只实现目的,如需理解原理还需查阅更深的资料。 1.实现的效果 2.创建排序的对象
ORDER BY _column1, _column2; /* _column1升序,_column2升序 */
我需要插入两个值 num1 = 50和 num2 = 80成一个已按升序排序的数组。我不能使用动态数组或列表。也没有结构或类。这是一个类作业,所以我必须遵循指导方针。教授建议我新建一个数组,newar
我是一名优秀的程序员,十分优秀!