从给定的数字列表中打印大小递增的连续子集对
Print consecutive pair of subsets with increasing size from a given list of numbers
例1,
输入:[a,b,c,d]
输出: [[a],[a,b],[b,c],[c,d],[a,b,c],[b ,c,d],[a,b,c,d]]
例二,
输入:[1,2,3,4,5]
输出: [[1],[1,2],[2,3],[3,4],[4,5],[1,2 ,3],[2,3,4],[3,4,5],[1,2,3,4],[2,3,4,5],[1,2,3,4,5 ]]
In同理,pair中的元素个数从1开始递增,直到'n'(给定列表的大小)
是否有可能的处理方式,任意大小的给定列表(如果可能,在Python中)
额外信息:
我 已经尝试过这段代码,输出中有 returns 对 3 个元素,但我想 return 第一个元素,接下来是2个元素,直到我上面例子中提到的n-1个元素
输入:
listA = [51,23,11,45]
res = [[listA[i], listA[i + 1],listA[i + 2]] for i in range(len(listA) - 2)]
print("List with paired elements: \n",res)
输出:
List with paired elements: [[51, 23, 11], [23, 11, 45]]
我对输出要求有点困惑,但这里有一些东西可以产生我认为你正在寻找的结果:
def get_list_of_size(l, n):
return [l[i:i+n] for i in range(len(l)-n+1)]
def get_consecutive_ss(l):
output=[]
for i in range(len(l)):
output+=get_list_of_size(l, i+1)
return [output[0]]+output[len(l):]
l = [1,2,3,4,5]
l2=['a','b','c','d']
print(get_consecutive_ss(l))
print(get_consecutive_ss(l2))
输出:
[[1], [1, 2], [2, 3], [3, 4], [4, 5], [1, 2, 3], [2, 3, 4], [3, 4, 5], [1, 2, 3, 4], [2, 3, 4, 5], [1, 2, 3, 4, 5]]
[['a'], ['a', 'b'], ['b', 'c'], ['c', 'd'], ['a', 'b', 'c'], ['b', 'c', 'd'], ['a', 'b', 'c', 'd']]
第一种方式
你可以使用包自动完成 more_itertools.substrings
:
import more_itertools
list(more_itertools.substrings([2,3,5,7,11]))
输出:
[(2,), (3,), (5,), (7,), (11,), (2, 3), (3, 5), (5, 7), (7, 11), (2, 3, 5), (3, 5, 7), (5, 7, 11), (2, 3, 5, 7), (3, 5, 7, 11), (2, 3, 5, 7, 11)]
你也可以查看该方法的实现源码,或者自己动手做一个练习。
第二种方式
它允许您使用 more_itertools.sliding_window
:
实现您所期望的
from itertools import chain
from more_itertools import sliding_window
x = [2,3,5,7,11]
first_part = [(x[0],)]
second_part = [sliding_window(x, n) for n in range(2, len(x)+1)]
list(chain(first_part, *second_part))
输出:
[(2,), (2, 3), (3, 5), (5, 7), (7, 11), (2, 3, 5), (3, 5, 7), (5, 7, 11), (2, 3, 5, 7), (3, 5, 7, 11), (2, 3, 5, 7, 11)]
例1,
输入:[a,b,c,d]
输出: [[a],[a,b],[b,c],[c,d],[a,b,c],[b ,c,d],[a,b,c,d]]
例二,
输入:[1,2,3,4,5]
输出: [[1],[1,2],[2,3],[3,4],[4,5],[1,2 ,3],[2,3,4],[3,4,5],[1,2,3,4],[2,3,4,5],[1,2,3,4,5 ]]
In同理,pair中的元素个数从1开始递增,直到'n'(给定列表的大小)
是否有可能的处理方式,任意大小的给定列表(如果可能,在Python中)
额外信息:
我 已经尝试过这段代码,输出中有 returns 对 3 个元素,但我想 return 第一个元素,接下来是2个元素,直到我上面例子中提到的n-1个元素
输入:
listA = [51,23,11,45]
res = [[listA[i], listA[i + 1],listA[i + 2]] for i in range(len(listA) - 2)]
print("List with paired elements: \n",res)
输出:
List with paired elements: [[51, 23, 11], [23, 11, 45]]
我对输出要求有点困惑,但这里有一些东西可以产生我认为你正在寻找的结果:
def get_list_of_size(l, n):
return [l[i:i+n] for i in range(len(l)-n+1)]
def get_consecutive_ss(l):
output=[]
for i in range(len(l)):
output+=get_list_of_size(l, i+1)
return [output[0]]+output[len(l):]
l = [1,2,3,4,5]
l2=['a','b','c','d']
print(get_consecutive_ss(l))
print(get_consecutive_ss(l2))
输出:
[[1], [1, 2], [2, 3], [3, 4], [4, 5], [1, 2, 3], [2, 3, 4], [3, 4, 5], [1, 2, 3, 4], [2, 3, 4, 5], [1, 2, 3, 4, 5]]
[['a'], ['a', 'b'], ['b', 'c'], ['c', 'd'], ['a', 'b', 'c'], ['b', 'c', 'd'], ['a', 'b', 'c', 'd']]
第一种方式
你可以使用包自动完成 more_itertools.substrings
:
import more_itertools
list(more_itertools.substrings([2,3,5,7,11]))
输出:
[(2,), (3,), (5,), (7,), (11,), (2, 3), (3, 5), (5, 7), (7, 11), (2, 3, 5), (3, 5, 7), (5, 7, 11), (2, 3, 5, 7), (3, 5, 7, 11), (2, 3, 5, 7, 11)]
你也可以查看该方法的实现源码,或者自己动手做一个练习。
第二种方式
它允许您使用 more_itertools.sliding_window
:
from itertools import chain
from more_itertools import sliding_window
x = [2,3,5,7,11]
first_part = [(x[0],)]
second_part = [sliding_window(x, n) for n in range(2, len(x)+1)]
list(chain(first_part, *second_part))
输出:
[(2,), (2, 3), (3, 5), (5, 7), (7, 11), (2, 3, 5), (3, 5, 7), (5, 7, 11), (2, 3, 5, 7), (3, 5, 7, 11), (2, 3, 5, 7, 11)]