如何在python中的嵌套列表旁边删除一个值的列表?

问题描述 投票:0回答:2

我的列表为

s=[[a,1,2,3],[2],[e,4],[r]]

如何删除仅在s内包含一个值的列表?请帮忙预先谢谢你

python list nested
2个回答
0
投票

更改逻辑-如果长度更像1,则过滤所有嵌套列表:

out = [x for x in s if len(x) > 1]

0
投票

是您想要的吗?

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]]

0
投票
[l for l in s if len(l) > 1]

是一种解决方案。这将删除所有长度为1的子列表。


0
投票

具有简单的列表理解:

>>> 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]]
© www.soinside.com 2019 - 2024. All rights reserved.