我看了很多并且阅读了很多问题,但是我无法弄清楚如何给sort方法的关键提供两个参数,所以我可以进行更复杂的比较.
例:
class FruitBox():
def __init__(self, weigth, fruit_val):
self.weigth = weigth
self.fruit_val = fruit_val
Run Code Online (Sandbox Code Playgroud)
我想通过fruit_val来比较FruitBox,但是!它们的箱子也比其他箱子大.
所以它会是:
f1 = FruitBox(2,5)
f2 = FruitBox(1,5)
f3 = FruitBox(2,4)
f4 = FruitBox(3,4)
boxes = [f1,f2,f3,f4]
boxes.sort(key = ???) # here is the question
Run Code Online (Sandbox Code Playgroud)
预期结果:
=>[FruitBox(2,4),FruitBox(3,4),FruitBox(1,5),FruitBox(2,5)]
有没有办法发送一个带有2个参数的函数,当我这样做的时候
def sorted_by(a,b):
#logic here, I don't know what will be yet
Run Code Online (Sandbox Code Playgroud)
而我呢
boxes.sort(key=sorted_by)
Run Code Online (Sandbox Code Playgroud)
它抛出:
Traceback (most recent call last):
File "python", line 15, in <module>
TypeError: sort_by_b() missing 1 required positional argument: 'b'
Run Code Online (Sandbox Code Playgroud)
如何给出排序键的两个参数?
该文档指定collections.Counter.most_common(),
相等计数的元素是任意排序的。
我对一种简洁的方法感兴趣,该方法首先按频率/值降序(默认)排序,然后按键升序排序。(键只是。中每个元组的第0个元素.most_common()。)
例:
from collections import Counter
arr1 = [1, 1, 1, 2, 2, 3, 3, 3, 5]
arr2 = [3, 3, 3, 1, 1, 1, 2, 2, 5] # Same values, different order
print(Counter(arr1).most_common())
print(Counter(arr2).most_common())
# [(1, 3), (3, 3), (2, 2), (5, 1)]
# [(3, 3), (1, 3), (2, 2), (5, 1)]
Run Code Online (Sandbox Code Playgroud)
所需的结果(对于arr2和arr2):
[(1, 3), (3, 3), (2, 2), (5, 1)]
Run Code Online (Sandbox Code Playgroud) 可以在Jinja2中连续应用排序过滤器,先按一个属性排序列表,然后再按另一个属性排序?这似乎是一件很自然的事情,但在我的测试中,前面的排序完全不稳定,所有排序都丢失了.
我已经通过在将列表传递给模板之前首先在python中进行排序来解决它,但我想知道是否可以在Jinja2中对"稳定"进行排序.
我应用过滤器的方式是这样的:
{{ item_list|sort(attribute='value')|sort(attribute='color') }}
Run Code Online (Sandbox Code Playgroud)
我希望看到的是列表排序依据color,元素共享一个颜色排序value.相反,它看起来好像value从未发生过排序.
例如,从此列表中:
2 red
3 blue
3 red
2 blue
1 blue
4 red
Run Code Online (Sandbox Code Playgroud)
我想要这样的东西:
1 blue
2 blue
3 blue
2 red
3 red
4 red
Run Code Online (Sandbox Code Playgroud)
我查看了groupby过滤器,但该实现看起来太复杂了(需要嵌套for循环).
如何按第一列升序和第二列降序对 NumPy 中的二维数组进行排序?
例如,
a = array([[9, 2, 3],
[4, 5, 6],
[7, 0, 5],
[7, 1, 6]])
Run Code Online (Sandbox Code Playgroud)
结果 :
array([[4, 5, 6],
[7, 1, 6],
[7, 0, 5],
[9, 2, 3]])
Run Code Online (Sandbox Code Playgroud) 我现在是python,对这种语言很新.
基于两个标准的排序列表给出一个单词列表,按照长度(最长到最短)的顺序返回具有相同单词的列表,第二个排序标准应该是按字母顺序排列的.提示:你需要考虑两个功能.
谁能帮我这个?
我想以相反的顺序对字符串列表进行排序,例如:
my_list = ['aaa', 'bbb', 'ccc']
Run Code Online (Sandbox Code Playgroud)
预期结果:
['ccc', 'bbb', 'aaa']
Run Code Online (Sandbox Code Playgroud)
我不想使用sorted(my_list, reverse=True),因为在更复杂的情况下,按两个值进行过滤将无法正常工作。例如:
my_list2 = [('aaa', 'bbb'), ('aaa', 'ccc'), ('bbb', 'aaa'), ('bbb', 'ccc')]
Run Code Online (Sandbox Code Playgroud)
预期结果将是:
[('bbb', 'aaa'), ('bbb', 'ccc'), ('aaa', 'bbb'), ('aaa', 'ccc')]
Run Code Online (Sandbox Code Playgroud)
sorted(my_list2, reverse=True) 返回:
[('bbb', 'ccc'), ('bbb', 'aaa'), ('aaa', 'ccc'), ('aaa', 'bbb')]
Run Code Online (Sandbox Code Playgroud)
用数字很简单,您可以取反值:
>>> my_list3 = [(1, 2), (1, 3), (2, 1), (2, 3)]
>>> sorted(my_list3, key=lambda x: (-x[0], x[1]))
... [(2, 1), (2, 3), (1, 2), (1, 3)]
Run Code Online (Sandbox Code Playgroud)
但是如何使用字符串呢?