class Card:
def __init__(self, value):
assert value >= 0 or value == -1, 'invalid entry'
self.value = value
self.face = str(self.value)
if self.value == -1:
self.face = '*'
else:
self.face = str(self.value)
def assign(self, value):
assert self.face == '*', 'not a wild card '
self.face = str(value)
self.value = value
def getValue(self):
return self.value
def getFace(self):
return self.face
def __str__(self):
return '[' + self.face + ']'
def __repr__(self):
return '.'+str(self.value)
class Hand:
# hand is a list of 5 cards in the players hand
def __init__(self, hand):
self.hand = hand
# self.hand.append(hand)
# self.hand=[]
def sort1(self):
# while self.hand:
# self.hand.append(min(self.hand.getValue()))
# self.hand.remove(min(self.hand))
# return self.hand
# self.hand.sort(key=lambda card: card.getValue())
# x=[]
# for i in self.hand:
# x.append(i.getValue())
return sorted(repr(self.hand))
# return sorted([i.getFace() for i in self.hand])
# return self.hand.sort()
手类的输入,这是纸牌类中的对象,我使用for循环创建15张卡并将其发送到手类。每个元素,即[2]是Card类中的一个对象,具有两个属性value和face
[[2][7][0][9][3][5][8][*][*][*][*][4][6][*][1]]
deck=[]
for j in range(0, 10):
for i in range(0, 1):
deck.append(Card(j))
for i in range(0, 5):
deck.append(Card(-1))
deck1 = shuffle(deck)
hand = Hand(deck1)
所需输出:
[[*][*][*][*][*][0][1][2][3][4][5][6][7][8][9]]
我需要将输入作为对象而不是它的整数或字符串表示形式,我尝试了各种似乎没有用的东西。
我想先基于整数排序,然后根据新列表的索引重新排列初始列表?
my_list = [["2"],["7"],["0"],["9"],["3"],["5"],["8"],["*"],["*"],["*"],["*"],["4"],["6"],["*"],["1"]]
print(sorted(my_list))
如果使用字符串表示形式,则应该根据您当前的描述进行操作