我的列表为
s=[[a,1,2,3],[2],[e,4],[r]]
如何删除仅在s内包含一个值的列表?请帮忙预先谢谢你
更改逻辑-如果长度更像1
,则过滤所有嵌套列表:
out = [x for x in s if len(x) > 1]
是您想要的吗?
a = e = r = 0
s = [[a, 1, 2, 3], [2], [e, 4], [r]]
results = [sub_list for sub_list in s if len(sub_list) > 1]
print(results)
输出:
[[0, 1, 2, 3], [0, 4]]
[l for l in s if len(l) > 1]
是一种解决方案。这将删除所有长度为1的子列表。
具有简单的列表理解:
>>> s=[['a',1,2,3],[2],['e',4],['r']]
>>> print([i for i in s if len(i) > 1])
>>> [['a', 1, 2, 3], ['e', 4]]