如何用一个循环顺序遍历多个列表?(复制)

时间:2021-08-10 18:22:30

This question already has an answer here:

这个问题已经有了答案:

In Python 3.6.3 Is there a way to loop though one list after another?

在Python 3.6.3中,是否有一种方法可以循环遍历一个又一个列表?

For example:

例如:

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

(In this case, I want to loop through the face cards right after the non-face cards.)

(在这种情况下,我想在非face cards之后对face cards进行循环。)

For clarification: The above line throws a:

澄清一下:上面这句话是:

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

This is my problem.

这是我的问题。

5 个解决方案

#1


17  

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:

range不返回Python3中的一个列表,所以range(2,10) + [J]、Q、K、a]不起作用,但是list(range(2,10)) + [J、Q、K、a]起作用。您还可以使用itertools。链连接iterable:

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"
]

The nested comprehension does, of course, constitute a cartesian product which you can also use a util for:

嵌套理解当然构成了笛卡尔积你也可以用util来表示:

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

#2


8  

The problem with your current code is here:

当前代码的问题是:

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

First off, it should be range(2, 11), otherwise, cards with the number 10 are omitted. Second, in order to join the range and the list, you'll have to do like so:

首先,它应该是量程(2,11),否则,数字为10的卡片将被省略。第二,为了加入范围和列表,你必须这样做:

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

So the final result will be:

所以最终的结果是:

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

I think this will give you the desired output (first all non-face cards, then all face cards).

我想这会给你想要的输出(首先是所有的非牌,然后是所有的牌)。

#3


5  

See @schwobaseggl's solution for what you want, but I usually prefer to represent cards as a 2 character string, however:

请参阅@schwobaseggl的解决方案以得到您想要的结果,但我通常更喜欢将纸牌表示为两个字符串:

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

This is more readable in my opinion, and will still behave a lot like a tuple of rank, suit.

在我看来,这更容易读懂,而且仍然会表现得很像一组等级,西装。

#4


3  

In Python3, you can use unpacking:

在Python3中,你可以使用unpacking:

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

#5


2  

Focusing only in the error you do get:

只关注你得到的错误:

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

类型错误:+:'range'和'list的不支持操作数类型

I will give you an explanation.

我会给你一个解释。

Your current code does work in Python 2:

您当前的代码在Python 2中工作:

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

will print:

将打印:

[(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')]

In Python 3 you have to use list(range(2, 11)):

在Python 3中,您必须使用列表(范围(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)

will print:

将打印:

[(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')]

You have to use list() because range() in Python 3 does create an immutable sequence type, not a list.

必须使用list(),因为Python 3中的range()确实创建了不可变的序列类型,而不是列表。

#1


17  

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:

range不返回Python3中的一个列表,所以range(2,10) + [J]、Q、K、a]不起作用,但是list(range(2,10)) + [J、Q、K、a]起作用。您还可以使用itertools。链连接iterable:

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"
]

The nested comprehension does, of course, constitute a cartesian product which you can also use a util for:

嵌套理解当然构成了笛卡尔积你也可以用util来表示:

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

#2


8  

The problem with your current code is here:

当前代码的问题是:

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

First off, it should be range(2, 11), otherwise, cards with the number 10 are omitted. Second, in order to join the range and the list, you'll have to do like so:

首先,它应该是量程(2,11),否则,数字为10的卡片将被省略。第二,为了加入范围和列表,你必须这样做:

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

So the final result will be:

所以最终的结果是:

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

I think this will give you the desired output (first all non-face cards, then all face cards).

我想这会给你想要的输出(首先是所有的非牌,然后是所有的牌)。

#3


5  

See @schwobaseggl's solution for what you want, but I usually prefer to represent cards as a 2 character string, however:

请参阅@schwobaseggl的解决方案以得到您想要的结果,但我通常更喜欢将纸牌表示为两个字符串:

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

This is more readable in my opinion, and will still behave a lot like a tuple of rank, suit.

在我看来,这更容易读懂,而且仍然会表现得很像一组等级,西装。

#4


3  

In Python3, you can use unpacking:

在Python3中,你可以使用unpacking:

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

#5


2  

Focusing only in the error you do get:

只关注你得到的错误:

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

类型错误:+:'range'和'list的不支持操作数类型

I will give you an explanation.

我会给你一个解释。

Your current code does work in Python 2:

您当前的代码在Python 2中工作:

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

will print:

将打印:

[(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')]

In Python 3 you have to use list(range(2, 11)):

在Python 3中,您必须使用列表(范围(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)

will print:

将打印:

[(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')]

You have to use list() because range() in Python 3 does create an immutable sequence type, not a list.

必须使用list(),因为Python 3中的range()确实创建了不可变的序列类型,而不是列表。