我有一个dicts列表:
b = [{u'TOT_PTS_Misc': u'Utley, Alex', u'Total_Points': 96.0},
{u'TOT_PTS_Misc': u'Russo, Brandon', u'Total_Points': 96.0},
{u'TOT_PTS_Misc': u'Chappell, Justin', u'Total_Points': 96.0},
{u'TOT_PTS_Misc': u'Foster, Toney', u'Total_Points': 80.0},
{u'TOT_PTS_Misc': u'Lawson, Roman', u'Total_Points': 80.0},
{u'TOT_PTS_Misc': u'Lempke, Sam', u'Total_Points': 80.0},
{u'TOT_PTS_Misc': u'Gnezda, Alex', u'Total_Points': 78.0},
{u'TOT_PTS_Misc': u'Kirks, Damien', u'Total_Points': 78.0},
{u'TOT_PTS_Misc': u'Worden, Tom', u'Total_Points': 78.0},
{u'TOT_PTS_Misc': u'Korecz, Mike', u'Total_Points': 78.0},
{u'TOT_PTS_Misc': u'Swartz, Brian', u'Total_Points': 66.0},
{u'TOT_PTS_Misc': u'Burgess, Randy', u'Total_Points': 66.0},
{u'TOT_PTS_Misc': u'Smugala, Ryan', u'Total_Points': 66.0},
{u'TOT_PTS_Misc': u'Harmon, Gary', u'Total_Points': 66.0},
{u'TOT_PTS_Misc': u'Blasinsky, Scott', u'Total_Points': 60.0},
{u'TOT_PTS_Misc': u'Carter …Run Code Online (Sandbox Code Playgroud) 我正在尝试将这个用python 2编写的代码转换为python 3
nums = ["30", "31"]
num.sort(cmp=lambda x, y: cmp(y + x, x + y))
Run Code Online (Sandbox Code Playgroud)
不知道如何在python 3中做到这一点,因为cmp被删除(我相信)
结果应该是["31", "30"]而不是["30", "31"]
在较新的Python中,我可以使用sorted函数,并根据它们的最后几个字符轻松地排序字符串列表:
lots_list=['anything']
print sorted(lots_list, key=returnlastchar)
def returnlastchar(s):
return s[10:]
Run Code Online (Sandbox Code Playgroud)
如何实现上述lots_list.sort()Python(2.3)中使用的上述内容?
"错误:当我尝试使用时sorted(),the global name sorted is not defined."
谢谢!
Python 3 删除了cmp排序函数的参数:
builtin.sorted()并且list.sort()不再接受cmp提供比较功能的参数。请改用key参数。
这对于仅通过检查序列中的单个项目即可确定的订单(例如key=str.lower)很好。但是,必须具有两个要检查的项目才能确定其订购的定制订购呢?
$ python2
Python 2.7.12+ (default, Sep 1 2016, 20:27:38)
[…]
>>> digits = ['3', '30', '34', '32', '9', '5']
>>> sorted(
... digits,
... cmp=(lambda a, b: cmp(a+b, b+a)),
... reverse=True)
['9', '5', '34', '3', '32', '30']
Run Code Online (Sandbox Code Playgroud)
那两个参数cmp函数不能用一个参数key函数代替吗?
假设我有一个比较器,它使用比较的两个值来决定排序。
例如,在这个问题中,我们必须使用一个使用两个元素的比较器函数:
def comparator(a, b):
ab = str(a) + str(b)
ba = str(b) + str(a)
return ((int(ba) > int(ab)) - (int(ba) < int(ab)))
Run Code Online (Sandbox Code Playgroud)
这个key=lambda x: ...在排序的时候可以写成格式吗?
我知道cmp_to_key存在将cmp函数转换为key函数的函数。我的问题是我们是否可以将其编写为键函数而不必以这种方式进行转换。
我想按照它们到原点(0,0)的距离按升序对2D坐标系中的点进行排序.我找到了这个并尝试了一些东西,但仍然无法得到理想的结果.
这是我的代码:
from functools import cmp_to_key
def my_comp(point1, point2):
return point1[0]*point1[0] + point1[1]*point1[1] < point2[0]*point2[0] + point2[1]*point2[1]
points = [ [3.1, 4.1], [0.9, 0.8], [1.0, 1.0] ]
sorted(points, key=cmp_to_key(my_comp))
print(points)
Run Code Online (Sandbox Code Playgroud)
结果:
[[3.1, 4.1], [0.9, 0.8], [1.0, 1.0]]
Run Code Online (Sandbox Code Playgroud)
预期:
[[0.9, 0.8], [1.0, 1.0], [3.1, 4.1]]
Run Code Online (Sandbox Code Playgroud) 这在 Python 2.7 中完美运行
def some_compare_func(x, y):
....
a = sorted(some_list, lambda x, y: some_compare_func(x, y))
Run Code Online (Sandbox Code Playgroud)
然而,在 Python 3.x 中同样会出现此错误。
TypeError: sorted expected 1 arguments, got 2
Run Code Online (Sandbox Code Playgroud)
有没有一种方法可以在 Python 2.7 和 3.x 中使用排序函数进行排序?
python ×7
python-3.x ×5
sorting ×5
python-2.7 ×2
comparator ×1
comparison ×1
python-2.3 ×1
string ×1