[英]Getting the indices of all non-None items from a sub-list in Python?
按照标题,我有一个像这样的嵌套列表(嵌套列表是固定长度的):
# ID, Name, Value
list1 = [[ 1, "foo", 10],
[ 2, "bar", None],
[ 3, "fizz", 57],
[ 4, "buzz", None]]
我想返回一个列表(项的数量等于list1
的子列表的长度),其中子列表是不带None作为第X项的行的索引,即:
[[non-None ID indices], [non-None Name indices], [non-None Value indices]]
以list1
为例,结果应为:
[[0, 1, 2, 3], [0, 1, 2, 3], [0, 2]]
我目前的实施是:
indices = [[] for _ in range(len(list1[0]))]
for i, row in enumerate(list1):
for j in range(len(row)):
if not isinstance(row[j], types.NoneType):
indices[j].append(i)
...这是可行的,但可能会很慢(列表的长度成千上万)。
有没有更好/更有效的方法来做到这一点?
编辑:
我已经将上述for循环重构为嵌套列表推导(类似于SilentGhost的答案)。 以下行给出的结果与我的原始实现相同,但运行速度提高了约10倍。
[[i for i in range(len(list1)) if list1[i][j] is not None] for j in range(len(log[0]))]
>>> [[i for i, j in enumerate(c) if j is not None] for c in zip(*list1)]
[[0, 1, 2, 3], [0, 1, 2, 3], [0, 2]]
在python-2.x中,可以使用itertools.izip
而不是zip
来避免生成中间列表。
[[i for i in range(len(list1)) if list1[i] is not None] for _ in range(len(log[0]))]
上面的内容似乎比我的原始帖子快10倍。
import numpy as np
map(lambda a: np.not_equal(a, None).nonzero()[0], np.transpose(list1))
# -> [array([0, 1, 2, 3]), array([0, 1, 2, 3]), array([0, 2])]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.