Python 根据if/else条件对列表进行排序
我需要根据条件对包含嵌套列表的列表进行排序。条件是:如果第一个索引为0,则按索引nr 7排序。然后,如果第二个索引为0,则按索引nr8排序。因此,以下列表(未排序): 应该是这样的:Python 根据if/else条件对列表进行排序,python,list,sorting,python-3.5,Python,List,Sorting,Python 3.5,我需要根据条件对包含嵌套列表的列表进行排序。条件是:如果第一个索引为0,则按索引nr 7排序。然后,如果第二个索引为0,则按索引nr8排序。因此,以下列表(未排序): 应该是这样的: [[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,
[[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]]
我尝试过使用_list.sort(key=itemgetter(0))和lambda,但是0总是首先结束。我已经用冒泡排序法解决了这个问题,但是列表中已经有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]
然后将此功能用作键
参数:
mylist.sort(key=sort_col_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]
然后将此功能用作键
参数:
mylist.sort(key=sort_col_value)
如果KOMPLETTITLista[i] [0 ]不是“没有”,那么如果“KOMPLETTILista[i](0)”和“Nealasas.LeBrcC不一定”,则“可以简化为”。e、 例如,您可能有一个
可选[int]
类型列表,其中None
表示缺少值,0
实际上是一个有效值。但在这种情况下,您可能是正确的。请澄清确切的排序标准。也有可能的重复:,并且显示了使用<代码> ITEMGETTHER /COD>,以及条件如“KOMPLETTITLista[i](0)不是一个,并且可以简化为”如果KOMPLETTILista[i](0)和“@ NeLasas.LeBeLc不一定”。e、 例如,您可能有一个可选[int]
类型列表,其中None
表示缺少值,0
实际上是一个有效值。但在这种情况下,您可能是正确的。请澄清确切的排序标准。还有一个可能的副本:,它显示了itemgetter
和def sort\u col_值(row)的用法:如果row[0]==0:return row[7]else:return row[0]
@ConSod,return操作符使else子句冗余。我在上面尝试了这个函数,但列表的排序与以前一样。我可以返回两个逻辑语句吗?或者我应该如何继续?没有足够的信息说明比较的逻辑是什么,所以你必须自己写。总体思路是编写一个函数,该函数接受一行并返回排序所需的值。谢谢!我通过返回一个具有所需条件的元组来解决问题。def sort\u col\u value(row):如果row[0]==0:返回row[7]else:return row[0]
@consd,return操作符使else子句冗余。我尝试了上述函数,但列表的排序与以前相同。我可以返回两个逻辑语句吗?或者我应该如何继续?没有足够的信息说明比较的逻辑是什么,所以你必须自己写。总体思路是编写一个函数,该函数接受一行并返回排序所需的值。谢谢!我通过返回一个具有所需条件的元组来解决这个问题。