python删除列表重复值_从Python列表中删除虚假值

python删除列表重复值

In python, the values that evaluate to False are considered Falsy values. The values are False, None, 0 and "".

在python中,评估为False的值被视为Falsy值。 值为False , None , 0和“” 。

Here, we are implementing a python program to remove the falsy values from a string. To remove these values we are using filter() method, it will filter out the falsy values.

在这里,我们正在实现一个python程序,以从字符串中删除伪造的值。 为了删除这些值,我们使用filter()方法,它将滤除伪造的值。

Example:

例:

    Input:
    [10, 20, 0, 30, 0, None]

    Output:
    [10, 20, 30]

    Input: 
    [False, None, 0, "", "Hello", 10, "Hi!"]
    
    Output: 
    ['Hello', 10, 'Hi!']

Program:

程序:

# Remove falsy values from a list in Python 
def newlist(lst):
  return list(filter(None, lst))

# main code
list1 = [10, 20, 0, 30, 0, None]
list2 = [40, False, "Hello", "", None]
list3 = [False, None, 0, "", "Hello", 10, "Hi!"]

# printing original strings
print("list1: ", list1)
print("list2: ", list2)
print("list3: ", list3)

# removing falsy values and printing
print("newlist(list1): ", newlist(list1))
print("newlist(list2): ", newlist(list2))
print("newlist(list3): ", newlist(list3))

Output

输出量

list1:  [10, 20, 0, 30, 0, None]
list2:  [40, False, 'Hello', '', None]
list3:  [False, None, 0, '', 'Hello', 10, 'Hi!']
newlist(list1):  [10, 20, 30]
newlist(list2):  [40, 'Hello']
newlist(list3):  ['Hello', 10, 'Hi!']


翻译自: https://www.includehelp.com/python/remove-falsy-values-from-a-list.aspx

python删除列表重复值

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值