如何查看我从列表中删除了多少项?

时间:2018-03-08 02:16:30

标签: python-3.x

我正在尝试查看已从列表中删除的项目数。正如你所知,当我分配总数时,我会得到多少仍然在列表中。我怎么办才能看到我拿出来的东西?

List = [1,2,3,4]
while True:
    removeItem = input()
    if removeItem in List:
        List.remove(removeItem
        continue
    elif removeItem == "":
        total = len(List)
        print(total)
        break

2 个答案:

答案 0 :(得分:0)

最简单的方法是在删除之前存储它。即:

List = [1,2,3,4]
before = len(List)

while True:
    removeItem = input()
    if removeItem in List:
        List.remove(removeItem
        continue
    elif removeItem == "":
        total = len(List)
        print(total)
        break

print before - len(list)

答案 1 :(得分:0)

您可以设置一个变量,以便在删除项目时通过递增来删除项目的计数。

list = [1, 2, 3, 4]
removed_count = 0
while True:
    print('Removed: %s' % removed_count)
    removeItem = input()
    if removeItem in List:
        List.remove(removeItem)
        removed_count += 1
        continue
    elif removeItem == "":
        total = len(List)
        print(total)
        break