简体   繁体   English

我要订购错误的清单吗

[英]am I going about ordering a list wrong

I have trying to generate a deck of cards that print out a list of 7 values and then I want to put the list into order. 我试图生成一副纸牌,这些纸牌可以打印出7个值的列表,然后将列表整理好。 I am a basic python coder and working my way through a python for beginners book I bought. 我是一个基本的python编码器,并且通过python为我购买的初学者本书工作。

import random
for x in range(7):
    rank = random.choice( ('A','2','3','4','5','6','7','8','9','T','J','Q','K') )
    suit = random.choice( ('c','d','h','s') )
    card = rank + suit
    print(card)

how could I put the list into order or am I completely wrong 我该如何整理清单,还是我完全错了

import random

#create an empty list
y = list()

# continue picking cards until you have 7 cards -> avoid duplicates
while len(y) < 7:
    rank = random.choice(('A','2','3','4','5','6','7','8','9','T','J','Q','K'))
    suit = random.choice( ('c','d','h','s') )
    card = rank + suit

    #if the card is not present in the list, add it 
    if not card in y:
        y.append(card)

        #sort your list (by 1st letter -> no sort criteria were given)
        y = sorted(y)

print(y)

Use itertools.product to generate the full deck of cards. 使用itertools.product生成完整的纸牌。 It basically gives you a sequence of every element of the first iterable with every element of the second iterable. 它基本上为您提供了第一个可迭代元素的序列以及第二个可迭代元素的序列。 You can then shuffle the deck using random.shuffle , just like you would a real deck. 然后,您可以使用random.shuffle对牌组进行random.shuffle ,就像在真正的牌组上一样。

Then take a slice of the first 7 elements and perform a sort on them. 然后对前7个元素进行切片,并对它们进行排序。

To sort you will need to provide a key function, as the natural sort order is not what you want. 要进行排序,您将需要提供key功能,因为自然排序顺序不是您想要的。 We can provide a tuple as a return value of the key function and it acts as a tie breaker for when the first character is the same. 我们可以提供一个tuple作为键函数的返回值,并且当第一个字符相同时,它可以作为平局决胜局。 What we want to sort on is the index of a particular character in the input tuple s ( rank , suit ). 我们要排序的是输入tuple s中的特定字符的索引( ranksuit )。

The first sorting key just uses the tuple.index method to get the indexes of the particular characters in the tuples . 第一个排序键仅使用tuple.index方法获取tuples特定字符的索引。 This is simple but it is slow, as it has to count up the tuple every time (O(n) or linear time complexity). 这很简单,但是很慢,因为每次都必须累加tuple (O(n)或线性时间复杂度)。 Those indices never change, so we can precompute them into a dict and get constant time look up in the key function, which is faster. 这些索引永远不会改变,因此我们可以将它们预先计算为dict并在键函数中获得恒定的时间查找,这是更快的。 For applications like this where the tuples are small and number of calculations are small the gains may not be so significant, so either solution is probably acceptable. 对于这样的应用,其中元组很小,计算量很小,那么增益可能不会那么重要,因此任何一种解决方案都可以接受。

I did the sort as rank first then suit. 我先按照排名排序,然后进行排序。 Your question doesnt specify either way, but perhaps sorting them suit is what you want? 您的问题没有指定任何一种方法,但是也许您想要对它们进行排序? In which case you should find it a useful exercise to convert the key functions. 在这种情况下,您应该找到转换关键功能的有用练习。

Here's the code 这是代码

from itertools import product
from random import choice, shuffle

rank = ('A','2','3','4','5','6','7','8','9','T','J','Q','K')
suit = ('c','d','h','s')

# make the full deck in the format of a 2 char string
deck = [r+s for r,s in product(rank, suit)]

# inplace shuffle of card
shuffle(deck)

# take the first 7 (we know there will be that many)
hand = deck[:7]

print(hand)

# sorting

# first way, slow but simpler
keyfn = lambda c: (rank.index(c[0]), suit.index(c[1]))
sortedhand = sorted(hand, key=keyfn)

print(sortedhand)

# second way, faster but more complex
def mkkeyfn():
    rankmap = {v: i for i, v in enumerate(rank)}
    suitmap = {v: i for i, v in enumerate(suit)}
    def keyfn(val):
        rank, suit = val
        return rankmap[rank], suitmap[suit]
    return keyfn

sortedhand2 = sorted(hand, key=mkkeyfn())
print(sortedhand2)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM