這提供了所需的解決方案。您可以使用內置的len,而不是``my_len」我的編輯這樣做過。
def my_len(sequence):
"""(sequence)->int
Return the length of a sequence e.g. lists, strings, e.t.c
"""
seq_length = 0
for i in sequence:
seq_length += 1
return seq_length
def remove(my_list, position):
"""(sequence)->sequence
Return a sequence with the item at position ``position' removed.
If the sequence length is larger (or smaller) than position,
remove the last element.
Dependencies: my_len
>>> xs = list(range(10))
>>> remove(xs, 5)
[0, 1, 2, 3, 5, 6, 7, 8, 9]
>>> remove(xs, 11)
[0, 1, 2, 3, 4, 5, 6, 7, 8]
>>> remove(xs, -5)
[1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> remove(xs, 0)
[1, 2, 3, 4, 5, 6, 7, 8, 9]
"""
lst = []
list_len = my_len(my_list)
if position <= 0:
rm_idx = 0
elif position >= list_len:
rm_idx = list_len - 1
else:
rm_idx = position - 1
for i in range(list_len):
if i != rm_idx:
lst += [my_list[i]]
return lst
像列表解析被允許的聲音,我 –
你嘗試過什麼到目前爲止 – shahkalpesh
聽起來像功課,我不應該問的完整解決方案 我們展示什麼。你已經完成並告訴我們你面臨的問題是什麼, –