我写了一个小程序,应该检查是否给定列表中的子表是从另一个列表中的子列表,并返回True
或False
:方案断言测试来检查,如果一个列表是从另一个列表中
def is_sublist_of(sublist, given):
""" Returns whether the sublist is part of the given combination.
The order of the sublist must also correspond to the order of the
corresponding part in the given combination."""
return sublist in [given[i:i+len(sublist)] for i in range(0,len(given)-len(sublist))]
此代码是一个任务,我必须做的,但给定的一个断言的部分是:
simple_list = [1, 2, 3, 4]
for element in simple_list:
assert is_sublist_of([element], simple_list)
assert not is_sublist_of([5], simple_list)
而我的计划未能做到这一点。这是否意味着我的程序在某些特殊情况下不起作用?感谢您关注此事。