编写程序,输入一个包含若干任意数据的列表,输出该列表中等价于True的元素(非零的值也判断为True)组成的值

编写程序,输入一个包含若干任意数据的列表,输出该列表中等价于True的元素(非零的值也判断为True)组成的值

lst = list(input().split())
lst1 = []
for i in lst:
    try:
        if int(i):
            lst1.append(i)
    except:
        if i:
            lst1.append(i)
print(" ".join(lst1))

lst = eval(input())
lst = [l for l in lst if l ]
print(lst)

img

>>> lst = list(map(eval, input().split()))
1 2 3 True False 0 None 'a'
>>> [i for i in lst if i]
[1, 2, 3, True, 'a']