我有一个列表列表,其中每个子列表包含一些整数:
o = [[1,2],[3,4],[2,3],[5,4]]
我想创建一个新的列表列表,在这个列表中,o
中共享一个公共成员的任何两个子列表都将被合并。这个合并过程应该持续到没有两个子列表共享一个公共元素为止。给定o
,我们将[1,2]
与{[1,2,3]
和{
集群o
的预期输出是[[1,2,3,4,5]]
我有一个预感,有一个方法,这是远远优于我目前的方法(见下文)。如果其他人能就如何最有效地(在时间上,然后在空间上)完成这项任务提出任何建议,我们将不胜感激。在
^{pr2}$
输出
^{pr2}$可以使用递归:
输出:
^{pr2}$相关问题 更多 >
编程相关推荐