Python 混合数值列表的排序列表
场景:包含具有各种值的列表记录的“行”的列表 问题:List.sort没有考虑数值,因此值会到处都是 i、 E9在80之后出现在列表中 我试过使用Python 混合数值列表的排序列表,python,list,sorting,numbers,Python,List,Sorting,Numbers,场景:包含具有各种值的列表记录的“行”的列表 问题:List.sort没有考虑数值,因此值会到处都是 i、 E9在80之后出现在列表中 我试过使用 list.sort(key=operator.itemgetter[index]) 做一个 list.sort(lambda x,y:int(x[index])<int(y[index])) list.sort(lambda x,y:int(x[index])使用转换后的数字作为键 L.sort(key=lambda x: int(x[i
list.sort(key=operator.itemgetter[index])
做一个
list.sort(lambda x,y:int(x[index])<int(y[index]))
list.sort(lambda x,y:int(x[index])使用转换后的数字作为键
L.sort(key=lambda x: int(x[index]))
使用转换后的数字作为键
L.sort(key=lambda x: int(x[index]))
这是正确的,但是operator.itemgetter是一个函数,因此语法是:
list.sort(key=operator.itemgetter(index))
或者,使用lambda:
list.sort(key=lambda x: x[index])
关键参数是要走的路,cmp参数已在Python 3中删除。如果仍要使用它,则应使用cmp()内置函数来实现比较器:
list.sort(cmp=lambda x, y: cmp(x[index], y[index]))
另请参见:您的思路是正确的,但是operator.itemgetter是一个函数,因此语法是:
list.sort(key=operator.itemgetter(index))
或者,使用lambda:
list.sort(key=lambda x: x[index])
关键参数是要走的路,cmp参数已在Python 3中删除。如果仍要使用它,则应使用cmp()内置函数来实现比较器:
list.sort(cmp=lambda x, y: cmp(x[index], y[index]))
另请参见:列表中的值是什么-我们需要更多信息列表中的值是什么-我们需要更多信息