使用列表理解从列表中删除多个元素的Python程序

We can remove elements from multiple indices in a list using this syntax,

我们可以使用以下语法从列表中的多个索引中删除元素,

    indices = index1, index2, ...
    list_name = [i for j, i in enumerate(list_name) if j not in indices]

Here, we are implementing a python program to remove multiple elements from a list using list comprehension.

在这里,我们正在实现一个python程序,以使用list comprehension从列表中删除多个元素

Example:

例:

    Input:
    list1 = [10, 20, 30, 40, 50, 60, 70]
    indices = 0, 2, 4
    Output:
    list1 = [20, 40, 60, 70]

    Input:
    list1 = [10, 20, 30, 40, 50, 60, 70]
    indices = 1, 3
    Output:
    list1 = [10, 30, 50, 60, 70]

Program:

程序:

# Python program to remove multiple elements 
# from a list using list comprehension

list1 = [10, 20, 30, 40, 50, 60, 70]

# printing the list
print("The list is: ")
print(list1)

# list comprehension, removing elements
indices = 0, 2, 4
list1 = [i for j, i in enumerate(list1) if j not in indices]

# printing the list after removeing elements
print("After removing elements, list is: ")
print(list1)

Output

输出量

The list is: 
[10, 20, 30, 40, 50, 60, 70]
After removing elements, list is: 
[20, 40, 60, 70]


翻译自: https://www.includehelp.com/python/program-to-remove-multiple-elements-from-a-list-using-list-comprehension.aspx

  • 3
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值