简体   繁体   English

如何从 Python 中的列表列表中删除索引?

[英]How do I remove a index from a list of lists in Python?

I am trying to remove a whole index in a list of lists (John in this case), as seen here where the list is called player_list :我正在尝试删除列表列表中的整个索引(在本例中为 John),如此处所示,列表名为player_list

player_list = [['Bruce Wayne', 5, 5, 0, 0, 100, 15], 
               ['Jessica Jones', 12, 0, 6, 6, 10, 6], 
               ['Johnny Rose', 6, 2, 0, 4, 20, 10], 
               ['Gina Linetti', 7, 4, 0, 3, 300, 15], 
               ['Buster Bluth', 3, 0, 2, 1, 50, 1], 
               ['John', 0, 0, 0, 0, 100, 0]]

However, when I run the function remove_player :但是,当我运行函数remove_player时:

def remove_player(player_list, name):
    check = find_player(player_list, name)
    new_list = []

    if check == -1:
        print(name, "is not found in players")
    else:
        for i in range(len(player_list)):
            if player_list[i] != player_list[check]:
                new_list.append(player_list[i])
        player_list = new_list
           
    print(name, "has sucessfully been removed.")

and the function: find_player :和功能: find_player

def find_player(player_list, name):
    found = -1
    index = 0

    while index < len(player_list) and found == -1:
        if name == player_list[index][0]:
            found = index
        index += 1
    return found
             

Where name comes from: name来自哪里:

removed_player = input("Please enter name: ")
remove = remove_player(player_list, removed_player)

It does remove the index from the list, however it still is printed in the table I have created here which is not what I want it to do:它确实从列表中删除了索引,但是它仍然打印在我在这里创建的表中,这不是我想要的:

===========================================================
-                     Player Summary                      -
===========================================================
-                            P  W  L  D    Chips    Score -
===========================================================
- Bruce Wayne                5  5  0  0      100       15 -
-----------------------------------------------------------
- Jessica Jones             12  0  6  6       10        6 -
-----------------------------------------------------------
- Johnny Rose                6  2  0  4       20       10 -
-----------------------------------------------------------
- Gina Linetti               7  4  0  3      300       15 -
-----------------------------------------------------------
- Buster Bluth               3  0  2  1       50        1 -
-----------------------------------------------------------
- John                       0  0  0  0      100        0 -
-----------------------------------------------------------
===========================================================

The function I have created for the table, display_players(player_list) :我为表创建的函数display_players(player_list)

def display_players(player_list):
    print("===========================================================")
    print("-", format("Player Summary", ">34s"), format("-", ">22s"))
    print("===========================================================")
    print("-", format("P", ">28s"), format("W", ">2s"), format("L", ">2s"), format("D", ">2s"), format("Chips", ">8s"), format("Score", ">8s"), format("-", ">1s"))
    print("===========================================================")

    for i in range(len(player_list)):
        print("-", format(str(player_list[i][0]), "<25s"), format(str(player_list[i][1]), ">2s"), format(str(player_list[i][2]), ">2s"), format(str(player_list[i][3]), ">2s"), format(str(player_list[i][4]), ">2s"), format(str(player_list[i][5]), ">8s"), format(str(player_list[i][6]), ">8s"), ("-"))
        print("-----------------------------------------------------------")

    print("===========================================================")

I'm not sure why it is not working.我不确定为什么它不起作用。 Please note I am not allowed to use any of the list functions aside from list_name.append(item) in the remove_player function.请注意,我不允许在 remove_player 函数中使用除list_name.append(item)之外的任何列表函数。

Thank you!谢谢!

Can you try out this?你能试试这个吗?

player_list = [['Bruce Wayne', 5, 5, 0, 0, 100, 15], 
               ['Jessica Jones', 12, 0, 6, 6, 10, 6], 
               ['Johnny Rose', 6, 2, 0, 4, 20, 10], 
               ['Gina Linetti', 7, 4, 0, 3, 300, 15], 
               ['Buster Bluth', 3, 0, 2, 1, 50, 1], 
               ['John', 0, 0, 0, 0, 100, 0]]

def remove_player(player_list, name):
    return [ player for player in player_list if player[0] != name]

player_list = remove_player(player_list, 'Bruce Wayne')

You can try to use the function pop which is going to delete the specific index.您可以尝试使用将删除特定索引的函数pop。

check = find_player(player_list, name)

if check == -1:
    print(name, "is not found in players")

else:
    player_list.pop(check)
print(name, "has sucessfully been removed.")

Here the doc这里的文档

list.pop([i])
Remove the item at the given position in the list, and return it. If no index is specified, a.pop() removes and returns the last item in the list. (The square brackets around the i in the method signature denote that the parameter is optional, not that you should type square brackets at that position. You will see this notation frequently in the Python Library Reference.)

Your code is except a little detail working fine.您的代码除了一些细节之外工作正常。 You never change the original player_list .你永远不会改变原来的player_list You use it in your function to make new_list and then you assign player_list to it, but all this is inside the function and you don't return it to get access to it outside of your function.您在函数中使用它来创建new_list ,然后将player_list分配给它,但所有这些都在函数内部,并且您不会返回它以在函数之外访问它。

Here is your remove_player function:这是您的 remove_player 功能:

def remove_player(player_list, name):
    check = find_player(player_list, name)
    new_list = []

    if check == -1:
        print(name, "is not found in players")
    else:
        for i in range(len(player_list)):
            if player_list[i] != player_list[check]:
                new_list.append(player_list[i])
        player_list = new_list
           
    print(name, "has sucessfully been removed.")
    return player_list

I only added the return statement at the end.我只在最后添加了return语句。

Now when calling this:现在调用这个时:

removed_player = input("Please enter name: ")
player_list = remove_player(player_list, removed_player)

assign the returned list to player_list and you will see, when printing it, it works.将返回的列表分配给player_list ,您将在打印时看到它有效。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM