2016-01-03 170 views
0

我有兩個Python列表之間的致意:重新排序的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

? –

+0

'zip'功能是你的朋友在這裏。 –

+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) 
+0

正是我以前的樣子。謝謝 – CiaranWelsh

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] 
1

您需要將兩個列表zip在一起,並將所得卡拉姆達指定要排序哪種方式進行排序。此代碼示例按卡片的第一項進行排序,即數字值。

numerical_sort = sorted(card for card in zip(numbers, suits), key = lambda card: card[0]) 
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'] 

第二個應該按順序排列訴訟發生的情況下,從多到少occurances列表

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'] 
如果你想保持*致意*爲什麼不把兩個列表組合成元組或字典的名單