range doesn't return a list in Python3, so range(2, 10) + ["J", "Q", "K", "A"] doesn't work, but list(range(2, 10)) + ["J", "Q", "K", "A"] does. You can also use itertools.chain to concatenate iterables:from itertools import chainchain(range(2, 10), ["J", "Q", "K", "A"])# or even shorter:chain(range(2, 10), "JQKA") # as strings themselves are iterables# so this comprehension will workdeck = [ (value, suit) for value in chain(range(2, 10), "JQKA") for suit in "HCDS"]当然,嵌套的理解确实构成了笛卡尔积,您也可以将util用于以下方面:The nested comprehension does, of course, constitute a cartesian product which you can also use a util for:from itertools import productdeck = list(product(chain(range(2, 10), "JQKA"), "HCDS")) 这篇关于如何通过一个循环依次遍历多个列表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云! 08-29 03:16