列表理解缺少一些值与写循环

时间:2016-04-29 19:07:14

标签: python

我有两个数据集,foundallowed。我想检查一下found中的任何观察是否都没有出现在allowed中,这样我就可以确保将它们标记为不被允许。

found = [["A", "B", "C"], [10, 20, 30], ["X", "Y", "Z"]]
allowed = [["A", "B", "C", "D"], [20, 30, 40], ["W", "X", "Y"]]

如果我运行以下内容,我会得到正确答案,即[(1, 10), (2, 'Z')]

new_values = []
for x in range(0, len(allowed)):
    for val in found[x]:
        if val not in allowed[x]:
            new_values.append((x, val))

但是,如果我运行以下操作,这需要更少的行,我认为更容易,它会返回new_values = [(2, 'Z')]

for x in range(0, len(allowed)):
    new_values = [(x, val) for val in found[x] if val not in allowed[x]]

(1, 10)发生了什么事?如果我运行10 in allowed[1],我会收到False,这让我认为它应该出现在new_values中!

3 个答案:

答案 0 :(得分:3)

您正在for循环中重新分配new_values

for x in range(0, len(allowed)):
    new_values = [(x, val) for val in found[x] if val not in allowed[x]]

试试这个:

new_values = []
for x in range(0, len(allowed)):
    new_values.extend([(x, val) for val in found[x] if val not in allowed[x]])

或者,使用列表推导更简短:

new_values = [(x, val) for x in range(0, len(allowed)) for val in found[x] if val not in allowed[x]]

答案 1 :(得分:2)

new_values = [(x, val) for x, allowable in enumerate(allowed)
                        for val in found[x] if val not in allowable]

如果你还需要值的索引,通常认为使用enumerate迭代序列会更加Pythonic。

答案 2 :(得分:2)

在嵌套式解析中插入enumerate可以缩短它:

new_values = [(i, x) for i, f in enumerate(found) for x in f if x not in allowed[i]]