我寫了一個小程序,應該檢查是否給定列表中的子表是從另一個列表中的子列表,並返回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)
而我的計劃未能做到這一點。這是否意味着我的程序在某些特殊情況下不起作用?感謝您關注此事。