简体   繁体   English

Python-从列表中删除列表

[英]Python - remove list from lists

I have a list from which I would like to delete a list so that I can have the required answer. 我有一个清单,我想从中删除清单,以便获得所需的答案。

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")

How can I delete list so that I get this answer: 如何删除列表,以便得到以下答案:

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

Then add a new list to get this result: 然后添加一个新列表以获得以下结果:

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

You would need to iterate through the list as the key that you are using to evaluate condition is in the sub list. 您将需要遍历列表,因为用于评估条件的关键字在子列表中。

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)

For a given example, where you want to specify bunch of properties for a name; 对于给定的示例,您要在其中指定名称的属性; I would have considered using dictionary with name as key. 我会考虑使用以名称为键的字典。

My recommendation would be to loop through the list of lists using nested while loops! 我的建议是使用嵌套while循环遍历列表列表! Check to see if the "name" you asked for is present at the index of the list, and if it is, pop or remove it using the location "[x[y]"(depending on what you want to do with the old/new list. I am a little unclear based on your phrasing). 检查您要查询的“名称”是否出现在列表的索引中,如果存在,请使用“ [x [y]””位置弹出或删除它(取决于您要使用旧名称/新列表。根据您的措辞,我不太清楚)。

Hope that helps! 希望有帮助!

Replace the index with what element to use in search. 用要在搜索中使用的元素替换索引。 (I assumed index=1 ) (我假设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]]

Recommended to use Dictionary for this types of data. 建议对此类数据使用字典。

Rather than a loop, why not filter() with a lambda function? 而不是循环,为什么不使用带有lambda函数的filter() Check it out: 看看这个:

>>> 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]]
>>>

https://docs.python.org/2/library/functions.html#filter for more information on filter() . 有关filter()更多信息, 请参见https://docs.python.org/2/library/functions.html#filter Hope this helps. 希望这可以帮助。

Edit: 编辑:

Didn't see your last question about adding another list to get the second answer. 没有看到您关于添加另一个列表以获取第二个答案的最后一个问题。 Try using the append() method. 尝试使用append()方法。

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

Don't use list as a variable name , its a type. 不要使用list作为变量名,它是一种类型。

You can simply use a function and use two parameters one for list and another for that word which you want to remove. 您可以简单地使用一个函数并使用两个参数,一个用于列表,另一个用于您要删除的单词。

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'))

output: 输出:

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

As you commented out: 如您所注释:

if we use name as input vaiable like name = input("Enter name: ").title() is it possible to delete a list in lists and same output as above 如果我们使用name作为输入变量,例如name = input(“ Enter name:”).title(),则可以删除列表中的列表以及与上述相同的输出

Here is the updated solution: 这是更新的解决方案:

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))

output: 输出:

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

If you want to remove this list of indices from a list of lists, you should try it by for and while loops: 如果要从列表列表中删除此索引列表,则应通过for和while循环进行尝试:

Index=[1,4,6,9]

list of lists: 清单清单:

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]]

The output would be: 输出为:

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]]

I use this code: at first sort list of indices: 我使用以下代码:首先对索引进行排序:

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