2016-10-18 10 views
0

条件に基づいてネストされたリストを含むリストをソートする必要があります。条件は次のとおりです。Python 3.5のif/else条件に基づいてリストのリストをソート

[[11, 37620, 'mat', 'xxx', 1, 28.5, 0, 11, 37620, 'Arp', 'xxx', 1, 28], 
[10, 24210, 'Skatt', 'xxx', 2, 40, 0, 0, 0, 0, 0, 0, 0], 
[10, 37010, 'test, a', 'xxx', 5, 36.75, 0, 10, 37010, '', 'xxx', 7, 50.75], 
[0, 0, 'mottagare', 'xxx', 3, 20.25, 0, 10, 21511, 0, 0, 0, 0],  
[10, 40000, 'eld', 'xxx', 5, 30.5, 0, 10, 40000, 'Oly', 'xxx', 5, 30], 
[10, 17060, 'bok', 'xxx', 1, 28.5, 0, 0, 0, 0, 0, 0, 0]] 

は次のようになります:2番目のインデックスは、ソートだから、インデックスNR 8(ソートされていない)以下のリストに、0である場合には、第1インデックスはその後ソートインデックスNR 7で、0であれば

[[10, 17060, 'bok', 'xxx', 1, 28.5, 0, 0, 0, 0, 0, 0, 0], 
[0, 0, 'mottagare', 'xxx', 3, 20.25, 0, 10, 21511, 0, 0, 0, 0], 
[10, 24210, 'Skatt', 'xxx', 2, 40, 0, 0, 0, 0, 0, 0, 0], 
[10, 40000, 'eld', 'xxx', 5, 30.5, 0, 10, 40000, 'Oly', 'xxx', 5, 30],  
[10, 37010, 'test, a', 'xxx', 5, 36.75, 0, 10, 37010, '', 'xxx', 7, 50.75], 
[11, 37620, 'mat', 'xxx', 1, 28.5, 0, 11, 37620, 'Arp', 'xxx', 1, 28]] 

私はa_list.sort(key = itemgetter(0))とラムダを試しましたが、ゼロは常に最初に終了します。私はこれをバブルソートで解決しましたが、すでにリストに6000個の要素が入っていて、非常に遅いです。私は参考のために以下のものを貼り付けます。リストゼロ値はもともとNoneですが、Pythonの3にソートするために、私はこれが大幅に高く評価されて解決するために0

the_list_pos = 0 
for key, value in kund_dict2.items(): 
    # Start sorting from the whole list, since the_list_pos is zero 
    for passnum in range(len(komplett_lista)-1,the_list_pos,-1): 
     # Loop the list, one by one 
     for i in range(passnum): 
      # If these helper variables are None at the end, nothing happens 
      comp1 = None 
      comp2 = None 
      # Check that the variables to compare are not None, and set them to appropiate values 
      if komplett_lista[i][0] is not None and komplett_lista[i][0] == key: 
       comp1 = komplett_lista[i][1] 
      elif komplett_lista[i][7] is not None and komplett_lista[i][7] == key: 
       comp1 = komplett_lista[i][8] 
      if komplett_lista[i+1][0] is not None and komplett_lista[i+1][0] == key: 
       comp2 = komplett_lista[i+1][1] 
      elif komplett_lista[i+1][7] is not None and komplett_lista[i+1][7] == key: 
       comp2 = komplett_lista[i+1][8] 
      # Do the actual sorting 
      if comp1 is not None and comp2 is not None:  
       if comp1 > comp2: 
        temp = komplett_lista[i] 
        komplett_lista[i] = komplett_lista[i+1] 
        komplett_lista[i+1] = temp 
    # update the position so that we do not sort over the already sorted data 
    the_list_pos += (value) 

どれアドバイスをintにそれらを変換しました!

+0

条件[i]の[ 0]はNoneではなく、komplett_lista [i] [0]と " –

+0

@ nicolas.leblancが必ずしもそうでない場合、"簡略化することができます。例えば ​​'Optional [int]'型のリストがあるかもしれません。ここで 'None'は欠損値を意味し、' 0'は実際には有効な値です。この特定の場合にはあなたは正しいかもしれません。 – acushner

+1

正確な並べ替え基準を明確にしてください。また、[リストのソート方法](http://stackoverflow.com/q/3121979/923794)、[2番目のパラメータに基づくタプルのソート](http://stackoverflow.com/q/item/8459231/923794)と、[第2の値(iow =タイブレーカー)に基づいて並べ替えられたリスト内の類似の値をソートする方法](http://stackoverflow.com/q/29551840/923794 ) – cfi

答えて

2

に並べ替えるためにどの列発見のためにあなたのロジックを格納する機能を書く:

def sort_col_value(row): 
    # if first index is 0, sort on index nr 7. 
    if row[0] == 0: 
     return row[7] 
    # Then if second index is 0, sort on index nr 8 
    if row[1] == 0: 
     return row[8] 
    return row[0] 

その後、keyパラメータとしてこの関数を使用します。komplett_lista場合」のような

mylist.sort(key=sort_col_value) 
+0

'DEF sort_col_value(行): 行なら[0] == 0: 戻り行他[7] : 戻り列[0]' – ConSod

+0

@ConSod、_return_オペレータは_else_句冗長 – volcano

+0

を行う私はこの機能を試しリストは以前と同じようにソートされます。 2つの論理的な文を返すことができますか、どのように進めるべきですか? – ConSod

関連する問題