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删除列表重复值