簡體   English   中英

Python-從列表中刪除列表

[英]Python - remove list from lists

我有一個清單,我想從中刪除清單,以便獲得所需的答案。

list = [["hello","son", 52],["welcome","home",65],["good","work",6]]
name = input("Name: ")
if name in list:
    del list[name]
    print("name + " deleted")
else:
    print("invalid name")

如何刪除列表,以便得到以下答案:

list = [["hello", "son", 52], ["good", "work", 6]]

然后添加一個新列表以獲得以下結果:

list = [["hello", "son", 52], ["good", "work", 6],["see","you","soon"]]

您將需要遍歷列表,因為用於評估條件的關鍵字在子列表中。

mylist = [["hello","son", 52],["welcome","home",65],["good","work",6]]
name = 'hello' # 'son' would also work
for i, sub_list in enumerate(mylist):
        if name in sub_list:
                del mylist[i]
                print(name + " deleted")
                break

print(mylist)

對於給定的示例,您要在其中指定名稱的屬性; 我會考慮使用以名稱為鍵的字典。

我的建議是使用嵌套while循環遍歷列表列表! 檢查您要查詢的“名稱”是否出現在列表的索引中,如果存在,請使用“ [x [y]””位置彈出或刪除它(取決於您要使用舊名稱/新列表。根據您的措辭,我不太清楚)。

希望有幫助!

用要在搜索中使用的元素替換索引。 (我假設index = 1)

>>> list = [["hello","son", 52],["welcome","home",65],["good","work",6]]
>>> name="home"
>>> list
[['hello', 'son', 52], ['welcome', 'home', 65], ['good', 'work', 6]]
>>> list = [rec for rec in list if rec[1] != name]
>>> list
[['hello', 'son', 52], ['good', 'work', 6]]

建議對此類數據使用字典。

而不是循環,為什么不使用帶有lambda函數的filter() 看看這個:

>>> l = [["hello","son", 52],["welcome","home",65],["good","work",6]]
>>> name = "home"
>>> filter(lambda sub_list: name not in sub_list, l)
[['hello', 'son', 52], ['good', 'work', 6]]
>>>

有關filter()更多信息, 請參見https://docs.python.org/2/library/functions.html#filter 希望這可以幫助。

編輯:

沒有看到您關於添加另一個列表以獲取第二個答案的最后一個問題。 嘗試使用append()方法。

>>> l = [["first", "list"]]
>>> m = ["second", "list"]
>>> l.append(m)
>>> l
[['first', 'list'], ['second', 'list']]
>>>

不要使用list作為變量名,它是一種類型。

您可以簡單地使用一個函數並使用兩個參數,一個用於列表,另一個用於您要刪除的單詞。

list_1 = [["hello","son", 52],["welcome","home",65],["good","work",6]]

def deleting(list_1,del_name):
    for sub_list in list_1:
        if del_name in sub_list:
            list_1.remove(sub_list)
    return list_1
print(deleting(list_1,'hello'))

輸出:

[['welcome', 'home', 65], ['good', 'work', 6]]

如您所注釋:

如果我們使用name作為輸入變量,例如name = input(“ Enter name:”).title(),則可以刪除列表中的列表以及與上述相同的輸出

這是更新的解決方案:

list_1 = [["hello","son", 52],["welcome","home",65],["good","work",6]]

def deleting(list_1):
    del_name=input("Enter name: ")
    for sub_list in list_1:
        if del_name in sub_list:
            list_1.remove(sub_list)
    return list_1
print(deleting(list_1))

輸出:

Enter name: hello
[['welcome', 'home', 65], ['good', 'work', 6]]

如果要從列表列表中刪除此索引列表,則應通過for和while循環進行嘗試:

Index=[1,4,6,9]

清單清單:

Coordinates=[[2,3,4,5],[6,7,8,9],[10,11,12,13],[14,15,16,17],[18,19,20,21],[22,23,24,25],[26,27,28,29],[30,31,32,33],[34,35,36,37],[38,39,40,41]]

輸出為:

Coordinates=[[2,3,4,5],[10,11,12,13],[14,15,16,17],[22,23,24,25],[30,31,32,33],[34,35,36,37]]

我使用以下代碼:首先對索引進行排序:

sorted(Index)
while(len(Index)>0):
    del Coordinates[Index[0]]
    del Index[0]
    for i in range(len(Index)):
        Index[i]-=1
print(Coordinates)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM