有没有办法可以为循环内联?
already_inserted = True
for i in indexes:
already_inserted = already_inserted and bitfield[i]
答案 0 :(得分:11)
already_inserted = all(bitfield[i] for i in indexes)
答案 1 :(得分:10)
怎么样:
already_inserted = all(bitfield[i] for i in indexes)
答案 2 :(得分:1)
all()函数接受iterable并自动遍历所有元素并将bool应用于每个元素。因此,写下来就足够了:
already_inserted = all(bitfield)