2017-09-24 26 views
-2

我的家庭作業中刪除一個元素是:如何從列表中索引在Python中不使用內置的功能

  • 編寫一個叫做remove(my list, position)功能是獲得一個列表,並作爲參數的位置。
  • 該函數返回列表的一個副本,該列表的副本由位置指定的索引處的項目返回,從列表中刪除。
  • 檢查超出列表(my list)範圍的位置值。
    • 如果位置大於列表長度,請移除列表末尾的項目。
    • 如果位置小於或等於零,請刪除存儲在列表開頭的項目。
  • 您必須在解決方案中使用循環。
  • 您必須使用:
    • 內置函數(除range()功能等),
    • 切片表情,
    • 列表方法,或
    • 字符串方法
    • 在解。

我只能用len()功能和range()

我不明白如何完成這項任務。

+1

像列表解析被允許的聲音,我 –

+0

你嘗試過什麼到目前爲止 – shahkalpesh

+2

聽起來像功課,我不應該問的完整解決方案 我們展示什麼。你已經完成並告訴我們你面臨的問題是什麼, –

回答

1

您可以通過使用範圍遍歷枚舉列表來創建一個沒有元素的新列表。

def remove(my_list, position): 
    l = len(my_list) 

    if position >= l: 
     position = l - 1 
    elif position <= 0: 
     position = 0 

    return [my_list[i] for i in range(l) if i != position] 

>>>remove([1,2,3,4], 5) 
[1, 2, 3] 
>>>remove([1,2,3,4], -1) 
[2, 3, 4] 
>>>remove([1,2,3,4], 1) 
[1, 3, 4] 
0

注:這裏作爲指數從1開始,位置憑此基於這一點。

def remove(l,pos): 
    pos-=1       #convert pos to starting index 0 
    size=len(l) 

    if pos <= 0: 
    return [l[i] for i in range(1,size)] 

    if pos >= size: 
    return [l[i] for i in range(size-1)] 

    return [l[i] for i in range(size) if i!=pos] 

#driver function 
l=[1,2,3,4] 

print(remove(l,0))    #If the position is less than or equal to zero, remove the item stored at the start of the list 
#[2, 3, 4] 

print(remove(l,1)) 
#[2, 3, 4] 

print(remove(l,2)) 
#[1, 3, 4] 

print(remove(l,5))    #If the position is greater than the length of the list, remove the item at the end of the list 
#[1, 2, 3] 

注2:len()這裏使用range()功能。

0

這提供了所需的解決方案。您可以使用內置的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 
+0

@StefanPochmann,Why ? –

+1

這兩個'i + = 1'語句是無操作 - for循環將'i'設置爲範圍中的下一個值,它不考慮當前值。評論這些陳述,看看它是否有任何區別。 – cdlane

+0

@StefanPochmann,謝謝,大腦放屁,我最初使用while循環來實現它。 +1發現錯誤,糾正並清理源代碼。 –

1

我們可以折騰range()只是另一種邪惡的內置功能和使用隱式循環避免複雜的理解而不是一個明確的一個:?

def remove(my_list, position): 
    length = len(my_list) 

    if position < 0: 
     position = 0 
    elif position >= length: 
     position = length - 1 

    def remove_recursive(my_list, position): 
     if my_list: 
      head, *tail = my_list 
      if position == 0: 
       return tail 
      return [head] + remove_recursive(tail, position - 1) 

     return my_list 

    return remove_recursive(my_list, position) 
相關問題