gpt4 book ai didi

python - 检索 Python 字典值

转载 作者:太空宇宙 更新时间:2023-11-03 18:40:11 24 4
gpt4 key购买 nike

我目前有一个带有相当大字典的程序。

cards = {1.1:"Ace of Spades",
1.2:"Ace of Clubs",
1.3:"Ace of Diamonds",
1.4:"Ace of Hearts",

2.1:"Two of Spades",
2.2:"Two of Clubs",
2.3:"Two of Diamonds",
2.4:"Two of Hearts",

3.1:"Three of Spades",
3.2:"Three of Clubs",
3.3:"Three of Diamonds",
3.4:"Three of Hearts",

4.1:"Four of Spades",
4.2:"Four of Clubs",
4.3:"Four of Diamonds",
4.4:"Four of Hearts",

5.1:"Five of Spades",
5.2:"Five of Clubs",
5.3:"Five of Diamonds",
5.4:"Five of Hearts",

6.1:"Six of Spades",
6.2:"Six of Clubs",
6.3:"Six of Diamonds",
6.4:"Six of Hearts",

7.1:"Seven of Spades",
7.2:"Seven of Clubs",
7.3:"Seven of Diamonds",
7.4:"Seven of Hearts",

8.1:"Eight of Spades",
8.2:"Eight of Clubs",
8.3:"Eight of Diamonds",
8.4:"Eight of Hearts",

9.1:"Nine of Spades",
9.2:"Nine of Clubs",
9.3:"Nine of Diamonds",
9.4:"Nine of Hearts",

10.1:"Ten of Spades",
10.2:"Ten of Clubs",
10.3:"Ten of Diamonds",
10.4:"Ten of Hearts",

11.1:"Jack of Spades",
11.2:"Jack of Clubs",
11.3:"Jack of Diamonds",
11.4:"Jack of Hearts",

12.1:"Queen of Spades",
12.2:"Queen of Clubs",
12.3:"Queen of Diamonds",
12.4:"Queen of Diamonds",

13.1:"King of Spades",
13.2:"King of Clubs",
13.3:"King of Diamonds",
13.4:"King of Hearts"}

我想要做的事情。例如打印出 11.2(梅花 jack )的相应值。然后我还想打印出梅花 jack (11.2) 的相应值。

我该怎么做?

编辑

另外我想补充一点,当然它不会每次都是相同的值。它将被改变。该程序生成一个 1 到 13 之间的随机数,然后生成另一个 1 到 4 之间的随机数,然后将这两个数字相加,这样它就是#.#,然后我想检索该 float 的相应值。

最佳答案

打印出随机选择的数字的相应值将很容易:

print cards[randomNumber]

当您想要进行反向查找并打印出与“Jack of Clubs”相对应的值或任何相应的卡名时,就会出现问题。对于传统的字典,这将需要很长时间,因为它将涉及搜索字典中的每个值。

为了避免这种情况,您可以创建两个字典。但是,使用 David Zwicker 链接到的 Two way/reverse map 问题中的建议之一会更有效。

关于python - 检索 Python 字典值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20698607/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com