根据 if/else 条件对列表列表进行排序
Sort a list of lists based on an if/else condition
我需要根据条件对包含嵌套列表的列表进行排序。条件是:如果第一个索引为 0,则按索引 nr 7 排序。如果第二个索引为 0,则按索引 nr 8 排序。所以下面的列表(未排序):
[[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]]
应该是这样的:
[[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)) 和 lambda,但零总是先结束。我已经通过冒泡排序解决了这个问题,但是列表中有 6000 个元素已经非常慢了。我粘贴下面的冒泡排序以供参考。列表零值最初是 None
,但为了在 Python 3 中排序,我已将它们转换为 int 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)
编写一个函数来包含查找要排序的列的逻辑:
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
参数:
mylist.sort(key=sort_col_value)
我需要根据条件对包含嵌套列表的列表进行排序。条件是:如果第一个索引为 0,则按索引 nr 7 排序。如果第二个索引为 0,则按索引 nr 8 排序。所以下面的列表(未排序):
[[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]]
应该是这样的:
[[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)) 和 lambda,但零总是先结束。我已经通过冒泡排序解决了这个问题,但是列表中有 6000 个元素已经非常慢了。我粘贴下面的冒泡排序以供参考。列表零值最初是 None
,但为了在 Python 3 中排序,我已将它们转换为 int 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)
编写一个函数来包含查找要排序的列的逻辑:
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
参数:
mylist.sort(key=sort_col_value)