我有两个Python列表:
numbers=[11, 12, 13, 10, 14, 2, 3]
suits=['D', 'D', 'D', 'D', 'D', 'C', 'C']
这些列表按特定顺序排列,即11表示D,2表示C等。
我试图编写一个函数来重新排列这些列表的顺序而不会打扰这些恭维(即11用D位做)。该函数有一个参数,用于确定列表的排序方式。第一个应该按照数字顺序排列列表,从最高到最低,第二个应该根据套装的实例排序,从列表中的大多数到最少出现(因此有5个D和2个C&这个例子,所以在这种情况下,它们已经按正确的顺序)。
此功能的结构粘贴在下面,但我正在努力解决代码的实际订购部分。有没有人对如何做到这一点有任何建议?
def order_cards(order_by=suit):
'''
order_by has two arguments, either suit or number
if suit: order by occurances of suit from largest to smallest
if number: order by decreasing number
'''
if order_by='number':
for i in range(len(numbers)): #Probably should iterate over the lists
#code for ordering cards
return ordered_cards
elif order_by='suit':
count=collections.Counter(suits) #This code count occurrences of suits
#code to order by number of suits
return ordered_cards
else:
return '{} is not \'suit\' or \'number\''.format(order_by)
答案 0 :(得分:3)
我想,你想要这样的东西:
>>> numbers=[11, 12, 13, 10, 14, 2, 3]
>>> suits=['D', 'D', 'D', 'D', 'D', 'C', 'C']
>>>
>>> t = zip(numbers,suits)
>>> t
[(11, 'D'), (12, 'D'), (13, 'D'), (10, 'D'), (14, 'D'), (2, 'C'), (3, 'C')]
>>>
>>>
>>> from operator import itemgetter
>>> sorted(t, key=itemgetter(0), reverse=True)#sort t by numbers from highest to lowest
[(14, 'D'), (13, 'D'), (12, 'D'), (11, 'D'), (10, 'D'), (3, 'C'), (2, 'C')]
>>> sorted(t, key=itemgetter(1)) #sort t by suits
[(2, 'C'), (3, 'C'), (11, 'D'), (12, 'D'), (13, 'D'), (10, 'D'), (14, 'D')]
>>> sorted(t, key=lambda s=itemgetter(0):suits.count(s[1]), reverse=True) #Sort by most occurence of cards
[(11, 'D'), (12, 'D'), (13, 'D'), (10, 'D'), (14, 'D'), (2, 'C'), (3, 'C')]
现在,在你的函数中应用上述内容:
from operator import itemgetter
def order_cards(order_by=suit):
'''
order_by has two arguments, either suit or number
if suit: order by occurances of suit from largest to smallest
if number: order by decreasing number
'''
t = zip(numbers,suits)
if order_by='number':
lst = sorted(t, key=itemgetter(0), reverse=True)
ordered_cards = [x[0] for x in lst]
return ordered_cards
elif order_by='suit':
lst = sorted(t, key=lambda s=itemgetter(0):suits.count(s[1]), reverse=True)
ordered_cards = [x[1] for x in t]
return ordered_cards
else:
return '{} is not \'suit\' or \'number\''.format(order_by)
答案 1 :(得分:2)
将列表压缩在一起以生成一个列表,其中包含组合元素的元组。这个压缩列表可以很容易地排序。如果您确实需要事后分离排序列表,请从已排序的组合列表中生成它们:
numbers = [11, 12, 13, 10, 14, 2, 3]
suits = ['D', 'D', 'D', 'D', 'D', 'C', 'C']
combined = sorted(zip(numbers,suits))
numbers = [n for n,s in combined]
suits = [s for n,s in combined]
答案 2 :(得分:1)
您需要同时zip
两个列表,并使用lambda对结果卡进行排序,以指定您要排序的方式。此代码示例按卡片的第一项排序,即数字值。
numerical_sort = sorted(card for card in zip(numbers, suits), key = lambda card: card[0])
答案 3 :(得分:1)
您可以使用zip
将列表操作为一个:
numbers=[11, 12, 13, 10, 14, 2, 3]
suits=['D', 'D', 'D', 'D', 'D', 'C', 'C']
combined = zip(numbers, suits) # [(11, 'D'), (12, 'D'), (13, 'D'), (10, 'D'), (14, 'D'), (2, 'C'), (3, 'C')]
第一个应该按照数字顺序排列列表,从最高到最低
sorted_list = sorted(combined, key= lambda x: x[0])
numbers = [fst for fst, _ in sorted_list] # [2, 3, 10, 11, 12, 13, 14]
suits = [snd for _, snd in sorted_list] # ['C', 'C', 'D', 'D', 'D', 'D', 'D']
第二个应该按照套装的实例来排序,从列表中的大多数到最少的出现
from collections import Counter
count = Counter(suits)
sorted_list = sorted(combined, key= lambda x: count[x[1]], reverse=True)
numbers = [fst for fst, _ in sorted_list] # [11, 12, 13, 10, 14, 2, 3]
suits = [snd for _, snd in sorted_list] # ['D', 'D', 'D', 'D', 'D', 'C', 'C']