将值赋给[string] / [list中的项]

时间:2021-03-05 20:52:20

I am creating a card game - blackjack - and need to display a string to the user, but keep track of the points that each card is worth for the calculations. So, I created a list with all the values of a card:

我正在创建一个纸牌游戏——blackjack——并需要向用户显示一个字符串,但是要跟踪每一张纸牌的点数,以便计算。因此,我创建了一个包含一张卡片所有值的列表:

cards = [1,1,1,1,2,2,2,2,3,3,3,3,...]

But to the user, for example I want to display King if the item in the list is 13. How would I make a dictionary / key or an array containing these values? I cannot use a dictionary based on position, as once the card is dealt, I will remove it from the list / deck.

但是对于用户,例如,如果列表中的项目是13,我想显示King。如何创建包含这些值的字典/键或数组?我不能使用基于位置的字典,因为一旦卡片被处理,我将从列表/甲板上删除它。

3 个解决方案

#1


1  

Internally, you can represent the cards as their values. The aces as 1, the kings as 13, etc...

在内部,您可以将这些卡片表示为它们的值。a是1,国王是13,等等…

You can construct your "deck" just as you'd expect. A list of [1,1,1,1, 2,2,2,...] and manipulate that however you want.

您可以按照预期构建您的“deck”。1、1、1、2、2、2、…你想怎么操作就怎么操作。

To actually display the card to the user, use the cardnames list Willem mentioned. This will work since the card value 1 will correspond to the card name in the first (zeroth) position of the cardnames list: "Ace".

要向用户实际显示该卡,请使用前面提到的cardnames列表。这将起作用,因为card值1将对应于cardnames列表的第一个(0)位置的cardname:“Ace”。

For example:

例如:

Python 3.4.1 |Anaconda 2.1.0 (64-bit)| (default, Sep 10 2014, 17:10:18) 
[GCC 4.4.7 20120313 (Red Hat 4.4.7-1)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> cardnames = ["Ace","2","3","4","5","6","7","8","9","10","Jack","Queen","King"]
>>> hand = [1,1,2,4,6,3,11,8]
>>> for x in hand:
...  print(cardnames[x-1])
... 
Ace
Ace
2
4
6
3
Jack
8

#2


3  

You simply define a list of strings:

您只需定义一个字符串列表:

cardnames = ["Ace","2","3","4","5","6","7","8","9","10","Jack","Queen","King"]

Now if the card has value v, you can get the corresponding name with cardnames[v-1]. For instance:

现在,如果该卡有值v,您可以得到相应的名称和卡号[v-1]。例如:

v = 5
print("I have a %s"%cardnames[v-1])

In that case the value for the jack is 11, for Queen 12 and for King 13. For instance (using python's interactive shell):

在这种情况下,jack的值是11,Queen 12和King 13。例如(使用python的交互式shell):

$ python
Python 2.7.9 (default, Apr  2 2015, 15:33:21) 
[GCC 4.9.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> cardnames = ["Ace","2","3","4","5","6","7","8","9","10","Jack","Queen","King"]
>>> v=12
>>> print("I have a %s"%cardnames[v-1])
I have a Queen
>>> v=7
>>> print("I have a %s"%cardnames[v-1])
I have a 7
>>> v=1
>>> print("I have a %s"%cardnames[v-1])
I have a Ace
>>> v=13
>>> print("I have a %s"%cardnames[v-1])
I have a King

Or using python3:

或者使用python3:

$ python3
Python 3.4.3 (default, Mar 26 2015, 22:03:40) 
[GCC 4.9.2] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> cardnames = ["Ace","2","3","4","5","6","7","8","9","10","Jack","Queen","King"]
>>> v=12
>>> print("I have a %s"%cardnames[v-1])
I have a Queen
>>> v=7
>>> print("I have a %s"%cardnames[v-1])
I have a 7
>>> v=1
>>> print("I have a %s"%cardnames[v-1])
I have a Ace
>>> v=13
>>> print("I have a %s"%cardnames[v-1])
I have a King

#3


1  

You could use a dictionary for the translation from number to card name. Like this:

你可以用字典把数字翻译成卡片的名字。是这样的:

faces = {
    1: 'Ace',
    2: '2',
    3: '3',
    ...
    11: 'Jack',
    12: 'Queen',
    13: 'King'
}

Then, you can call them accordingly. Like:

然后,你可以相应地打电话给他们。如:

faces[cardnames[13]]

would equal "King"

相当于“国王”

To elaborate a bit, since there were question in the comments:

我来详细说明一下,因为评论中有问题:

a dict (which is what faces is) is a set of key value pairs. In this case, the "key" is the numeric value of the card and the "value" is the face (string) of the card. You reference a value from a dict by it's key.

命令(即faces)是一组键值对。在本例中,“key”是该卡的数值,“value”是该卡的面(字符串)。您可以通过它的键从一个命令中引用一个值。

For example. If I have a dict like the one above, and I say:

为例。如果我有一个像上面那样的法令,我说:

faces[1]

And that would be 'Ace'. For example:

那就是a。例如:

>>> faces = {1: 'Ace', 11: 'Jack', 12: 'Queen', 13: 'King'}
>>> faces[1]
'Ace'
>>> faces[13]
'King'
>>> cards = [1,1,1,1,2,2,2,2,3,3,3,3,11]
>>> cards[1]
1
>>> faces[cards[1]]
'Ace'

#1


1  

Internally, you can represent the cards as their values. The aces as 1, the kings as 13, etc...

在内部,您可以将这些卡片表示为它们的值。a是1,国王是13,等等…

You can construct your "deck" just as you'd expect. A list of [1,1,1,1, 2,2,2,...] and manipulate that however you want.

您可以按照预期构建您的“deck”。1、1、1、2、2、2、…你想怎么操作就怎么操作。

To actually display the card to the user, use the cardnames list Willem mentioned. This will work since the card value 1 will correspond to the card name in the first (zeroth) position of the cardnames list: "Ace".

要向用户实际显示该卡,请使用前面提到的cardnames列表。这将起作用,因为card值1将对应于cardnames列表的第一个(0)位置的cardname:“Ace”。

For example:

例如:

Python 3.4.1 |Anaconda 2.1.0 (64-bit)| (default, Sep 10 2014, 17:10:18) 
[GCC 4.4.7 20120313 (Red Hat 4.4.7-1)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> cardnames = ["Ace","2","3","4","5","6","7","8","9","10","Jack","Queen","King"]
>>> hand = [1,1,2,4,6,3,11,8]
>>> for x in hand:
...  print(cardnames[x-1])
... 
Ace
Ace
2
4
6
3
Jack
8

#2


3  

You simply define a list of strings:

您只需定义一个字符串列表:

cardnames = ["Ace","2","3","4","5","6","7","8","9","10","Jack","Queen","King"]

Now if the card has value v, you can get the corresponding name with cardnames[v-1]. For instance:

现在,如果该卡有值v,您可以得到相应的名称和卡号[v-1]。例如:

v = 5
print("I have a %s"%cardnames[v-1])

In that case the value for the jack is 11, for Queen 12 and for King 13. For instance (using python's interactive shell):

在这种情况下,jack的值是11,Queen 12和King 13。例如(使用python的交互式shell):

$ python
Python 2.7.9 (default, Apr  2 2015, 15:33:21) 
[GCC 4.9.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> cardnames = ["Ace","2","3","4","5","6","7","8","9","10","Jack","Queen","King"]
>>> v=12
>>> print("I have a %s"%cardnames[v-1])
I have a Queen
>>> v=7
>>> print("I have a %s"%cardnames[v-1])
I have a 7
>>> v=1
>>> print("I have a %s"%cardnames[v-1])
I have a Ace
>>> v=13
>>> print("I have a %s"%cardnames[v-1])
I have a King

Or using python3:

或者使用python3:

$ python3
Python 3.4.3 (default, Mar 26 2015, 22:03:40) 
[GCC 4.9.2] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> cardnames = ["Ace","2","3","4","5","6","7","8","9","10","Jack","Queen","King"]
>>> v=12
>>> print("I have a %s"%cardnames[v-1])
I have a Queen
>>> v=7
>>> print("I have a %s"%cardnames[v-1])
I have a 7
>>> v=1
>>> print("I have a %s"%cardnames[v-1])
I have a Ace
>>> v=13
>>> print("I have a %s"%cardnames[v-1])
I have a King

#3


1  

You could use a dictionary for the translation from number to card name. Like this:

你可以用字典把数字翻译成卡片的名字。是这样的:

faces = {
    1: 'Ace',
    2: '2',
    3: '3',
    ...
    11: 'Jack',
    12: 'Queen',
    13: 'King'
}

Then, you can call them accordingly. Like:

然后,你可以相应地打电话给他们。如:

faces[cardnames[13]]

would equal "King"

相当于“国王”

To elaborate a bit, since there were question in the comments:

我来详细说明一下,因为评论中有问题:

a dict (which is what faces is) is a set of key value pairs. In this case, the "key" is the numeric value of the card and the "value" is the face (string) of the card. You reference a value from a dict by it's key.

命令(即faces)是一组键值对。在本例中,“key”是该卡的数值,“value”是该卡的面(字符串)。您可以通过它的键从一个命令中引用一个值。

For example. If I have a dict like the one above, and I say:

为例。如果我有一个像上面那样的法令,我说:

faces[1]

And that would be 'Ace'. For example:

那就是a。例如:

>>> faces = {1: 'Ace', 11: 'Jack', 12: 'Queen', 13: 'King'}
>>> faces[1]
'Ace'
>>> faces[13]
'King'
>>> cards = [1,1,1,1,2,2,2,2,3,3,3,3,11]
>>> cards[1]
1
>>> faces[cards[1]]
'Ace'