鍍金池/ 問(wèn)答/Python/ 這個(gè)python語(yǔ)句是什么意思?

這個(gè)python語(yǔ)句是什么意思?

下面函數(shù)中的第二行這個(gè)語(yǔ)句是什么意思?

def item_completed(self, results, item, info):
    image_paths = [x['path'] for ok, x in results if ok]  # 中括號(hào)里面的語(yǔ)句是什么意思?
    if not image_paths:
        raise DropItem("Item contains no images")
    item['image_paths'] = image_paths
    return item
回答
編輯回答
憶當(dāng)年

列表生成式,上面是將results的值分別給x,ok,如果ok的值為T(mén)rue,那么就取x['path']最后形成一個(gè)一個(gè)list

2017年9月7日 03:45
編輯回答
傲寒

這叫列表生成式,見(jiàn)http://python.jobbole.com/80823/

2018年6月18日 04:06