删除列表列表中的空列表元素
Removing the empty list element within a list of lists
我有一个嵌套列表,如下所示。
[['eggplant', ['little', 'added']],
['proportions', ['limited', 'vegetables']],
['vegetables', ['proportions']],
['foods', ['other']],
['meat', []],
['starch', []]]
我正在尝试过滤掉具有与之关联的 [](空)元素的列表。所以我想要的输出是
[['eggplant', ['little', 'added']],
['proportions', ['limited', 'vegetables']],
['vegetables', ['proportions']],
['foods', ['other']]]
快速完成用作过滤器的列表理解:
a = [['eggplant', ['little', 'added']],
['proportions', ['limited', 'vegetables']],
['vegetables', ['proportions']],
['foods', ['other']],
['meat', []],
['starch', []]]
b = [x for x in a if x[1]]
for x in b:
print(x)
打印:
['eggplant', ['little', 'added']]
['proportions', ['limited', 'vegetables']]
['vegetables', ['proportions']]
['foods', ['other']]
我有一个嵌套列表,如下所示。
[['eggplant', ['little', 'added']],
['proportions', ['limited', 'vegetables']],
['vegetables', ['proportions']],
['foods', ['other']],
['meat', []],
['starch', []]]
我正在尝试过滤掉具有与之关联的 [](空)元素的列表。所以我想要的输出是
[['eggplant', ['little', 'added']],
['proportions', ['limited', 'vegetables']],
['vegetables', ['proportions']],
['foods', ['other']]]
快速完成用作过滤器的列表理解:
a = [['eggplant', ['little', 'added']],
['proportions', ['limited', 'vegetables']],
['vegetables', ['proportions']],
['foods', ['other']],
['meat', []],
['starch', []]]
b = [x for x in a if x[1]]
for x in b:
print(x)
打印:
['eggplant', ['little', 'added']]
['proportions', ['limited', 'vegetables']]
['vegetables', ['proportions']]
['foods', ['other']]