如何使用一个循环顺序循环遍历多个列表?

时间:2017-11-26 19:01:10

标签: python python-3.x loops

Python 3.6.3 中是否有办法循环使用一个列表?

例如:

deck = [(value, suit) for value in range(2, 11) +
            ["J", "Q", "K", "A"] for suit in ["H", "C", "D", "S"]]

(在这种情况下,我想在非面部卡片后面循环使用面部卡片。)

澄清: 上面的一行抛出了:

TypeError: unsupported operand type(s) for +: 'range' and 'list'

这是我的问题。

5 个答案:

答案 0 :(得分:17)

range在Python3中未返回list,因此range(2, 10) + ["J", "Q", "K", "A"]不起作用,但list(range(2, 10)) + ["J", "Q", "K", "A"]不起作用。您还可以使用itertools.chain来连接迭代:

from itertools import chain 

chain(range(2, 10), ["J", "Q", "K", "A"])
# or even shorter:
chain(range(2, 10), "JQKA")  # as strings themselves are iterables

# so this comprehension will work
deck = [
   (value, suit) 
   for value in chain(range(2, 10), "JQKA") 
   for suit in "HCDS"
]

嵌套理解当然构成了一个笛卡尔积,你也可以使用它来实现:

from itertools import product
deck = list(product(chain(range(2, 10), "JQKA"), "HCDS"))

答案 1 :(得分:8)

您当前代码的问题在于:

grid.Children.Add(item,left,top)

首先,它应该是range(2, 10) + ["J", "Q", "K", "A"] ,否则,省略号码为10的卡片。其次,为了加入范围和列表,您必须这样做:

range(2, 11)

所以最终结果将是:

list(range(2, 11)) + ["J", "Q", "K", "A"]

我认为这会给你想要的输出(首先是所有非面部卡,然后是所有面部卡)。

答案 2 :(得分:5)

请参阅@ schwobaseggl的解决方案,但我通常更喜欢将卡片表示为2个字符的字符串,但是:

deck = [r+s for r in '23456789TJQKA' for s in 'hcds']

在我看来,这更具可读性,并且仍然会像排名,西装那样表现得很好。

答案 3 :(得分:3)

在Python3中,您可以使用解包:

deck = [(value, suit) for value in [*range(2, 10), "J", "Q", "K", "A"] for suit in ["H", "C", "D", "S"]]

答案 4 :(得分:2)

只关注你得到的错误:

  

TypeError:+:'range'和'list

的不支持的操作数类型

我会给你一个解释。

您当前的代码适用于 Python 2

...
  <Link
    text="clickMe"
    press="onClickMethod">
  </Link>
...

将打印:

deck = [(value, suit) for value in range(2, 11) + ["J", "Q", "K", "A"] for suit in ["H", "C", "D", "S"]]
print deck

Python 3 中,您必须使用[(2, 'H'), (2, 'C'), (2, 'D'), (2, 'S'), (3, 'H'), (3, 'C'), (3, 'D'), (3, 'S'), (4, 'H'), (4, 'C'), (4, 'D'), (4, 'S'), (5, 'H'), (5, 'C'), (5, 'D'), (5, 'S'), (6, 'H'), (6, 'C'), (6, 'D'), (6, 'S'), (7, 'H'), (7, 'C'), (7, 'D'), (7, 'S'), (8, 'H'), (8, 'C'), (8, 'D'), (8, 'S'), (9, 'H'), (9, 'C'), (9, 'D'), (9, 'S'), (10, 'H'), (10, 'C'), (10, 'D'), (10, 'S'), ('J', 'H'), ('J', 'C'), ('J', 'D'), ('J', 'S'), ('Q', 'H'), ('Q', 'C'), ('Q', 'D'), ('Q', 'S'), ('K', 'H'), ('K', 'C'), ('K', 'D'), ('K', 'S'), ('A', 'H'), ('A', 'C'), ('A', 'D'), ('A', 'S')]

list(range(2, 11))

将打印:

deck = [(value, suit) for value in list(range(2, 11)) + ["J", "Q", "K", "A"] for suit in ["H", "C", "D", "S"]]
print(deck)

您必须使用[(2, 'H'), (2, 'C'), (2, 'D'), (2, 'S'), (3, 'H'), (3, 'C'), (3, 'D'), (3, 'S'), (4, 'H'), (4, 'C'), (4, 'D'), (4, 'S'), (5, 'H'), (5, 'C'), (5, 'D'), (5, 'S'), (6, 'H'), (6, 'C'), (6, 'D'), (6, 'S'), (7, 'H'), (7, 'C'), (7, 'D'), (7, 'S'), (8, 'H'), (8, 'C'), (8, 'D'), (8, 'S'), (9, 'H'), (9, 'C'), (9, 'D'), (9, 'S'), (10, 'H'), (10, 'C'), (10, 'D'), (10, 'S'), ('J', 'H'), ('J', 'C'), ('J', 'D'), ('J', 'S'), ('Q', 'H'), ('Q', 'C'), ('Q', 'D'), ('Q', 'S'), ('K', 'H'), ('K', 'C'), ('K', 'D'), ('K', 'S'), ('A', 'H'), ('A', 'C'), ('A', 'D'), ('A', 'S')] ,因为Python 3中的 range()确实会创建一个不可变的序列类型,而不是列表。