Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/silverlight/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 打印列表的名称_Python_Arrays_Python 3.x_List - Fatal编程技术网

Python 打印列表的名称

Python 打印列表的名称,python,arrays,python-3.x,list,Python,Arrays,Python 3.x,List,我的问题是potentialsuit.title部分打印整个列表,而我只想打印列表名称。我知道我写的部分不能解决这个问题,但那只是一个替代品。这不起作用,因为列表和其他Python对象一样没有名称 想象一下以下场景: import random Diamonds = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, "Jack", "Queen", "King"] Hearts = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, "Jack", "Queen", "

我的问题是potentialsuit.title部分打印整个列表,而我只想打印列表名称。我知道我写的部分不能解决这个问题,但那只是一个替代品。

这不起作用,因为列表和其他Python对象一样没有名称

想象一下以下场景:

import random
Diamonds = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, "Jack", "Queen", "King"]
Hearts = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, "Jack", "Queen", "King"]
Clubs = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, "Jack", "Queen", "King"]
Spades = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, "Jack", "Queen", "King"]
suitvalues = [Diamonds, Hearts, Clubs, Spades]
potentialsuit = random.choice(suitvalues)
potentialcard = random.choice(potentialsuit)
print(potentialcard ,"of" ,potentialsuit.title)
y、 它不仅仅是x的一个副本,而是指同一个列表,通过询问x是y,你可以看到,这个列表的名称和x一样有效。那么…标题应该选择哪个名称呢

解决问题的众多方法之一是将卡片存储在字典中:

x = [1, 2, 3]
y = x
listsuits利用了这样一个事实,即在字典上迭代会产生它的键。潜在诉讼将不再是一张卡片价值清单,而是诉讼的名称,例如俱乐部。然后,第二个选择选择一套[俱乐部],这是卡值列表

想一想,像这样随机选择一张牌是没有意义的;你不需要这份清单的四份副本。相反,以下内容就足够了:

import random

suits = {
    "Diamonds": [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, "Jack", "Queen", "King"], 
    "Hearts": [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, "Jack", "Queen", "King"], 
    "Clubs": [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, "Jack", "Queen", "King"], 
    "Spades": [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, "Jack", "Queen", "King"],
}

potentialsuit = random.choice(list(suits))
potentialcard = random.choice(suits[potentialsuit])
print(potentialcard, "of", potentialsuit)

我不知道如何打印变量的名称,但我有两个想法可以帮助您:

你也许能在这方面找到帮助。 我的建议是,如果你的案例与你写的例子相似,那么你应该使用字典,就像L3viathan一样。
这将为您解决此问题:

import random
suit = random.choice(["Diamonds", "Hearts", "Clubs", "Spades"])
value = random.choice([1, 2, 3, 4, 5, 6, 7, 8, 9, 10, "Jack", "Queen", "King"])
print(value, "of", suit)
每次输出都会不同,您将在其中获得整数,而不需要长的拆分选择代码

以下是我获得的前3个输出:

import random

face_card = ['King', 'Queen', 'Jack']
suit_card = ['Diamonds', 'Clubs', 'Spades', 'Hearts']
for x in range(1):
    choice = random.randint(1, 10)
    face_card.append(choice)  # Inserts integer randomly in to list
ran_fcard = random.choice(face_card)
ran_scard = random.choice(suit_card)

print('The card chosen is {} of {}'.format(ran_fcard, ran_scard))
face_card = ['King', 'Queen', 'Jack'] # Resets list to face cards

当我运行这个程序时,我得到了属性错误:“list”对象没有属性“title”可能是重复的,我并不认为它是重复的。关于Python变量如何工作,这是一个不同的问题!您需要将西装的名称与等级一起存储。
The card chosen is 6 of Spades
The card chosen is King of Diamonds
The card chosen is 10 of Spades