I have tried to figure this out in different ways, to no success. I keep getting ascending order sort, rather than descending order when I print.
我试图以不同的方式解决这个问题,但没有成功。我不断按升序排序,而不是在打印时按降序排序。
ListB = [24, 13, -15, -36, 8, 22, 48, 25, 46, -9]
sorted(ListB, key=int, reverse=True)
print sorted(ListB)
5 个解决方案
#1
8
You are printing the list sorted ascending:
您正在打印以升序排序的列表:
print sorted(ListB)
If you want it descending, put your print statement on the previous line (where you reverse it)
如果您希望它降序,请将您的print语句放在上一行(您将其反转)
print sorted(ListB, key=int, reverse=True)
Then remove your final print statement.
然后删除最终的打印声明。
Example:
例:
>>> ListB = [24, 13, -15, -36, 8, 22, 48, 25, 46, -9]
>>> print sorted(ListB, key=int, reverse=True)
[48, 46, 25, 24, 22, 13, 8, -9, -15, -36]
#2
10
Try this, it'll sort the list in-place in descending order (there's no need to specify a key in this case):
试试这个,它会按降序排列列表(在这种情况下不需要指定键):
listB = [24, 13, -15, -36, 8, 22, 48, 25, 46, -9]
listB.sort(reverse=True) # listB gets modified
print listB
=> [48, 46, 25, 24, 22, 13, 8, -9, -15, -36]
Alternatively, you can create a new sorted list:
或者,您可以创建新的排序列表:
listB = [24, 13, -15, -36, 8, 22, 48, 25, 46, -9]
listC = sorted(listB, reverse=True) # listB remains untouched
print listC
=> [48, 46, 25, 24, 22, 13, 8, -9, -15, -36]
#3
2
ListB = [24, 13, -15, -36, 8, 22, 48, 25, 46, -9]
ListB = sorted(ListB, key=int, reverse=True)
print ListB
Sorted does not change the variable passed to it. So if you want to do anything with them you have to store sorted output into a variable.
Sorted不会更改传递给它的变量。因此,如果您想对它们执行任何操作,则必须将已排序的输出存储到变量中。
#4
0
reversed(sorted(listb))
This creates an iterable going from 48 -> -36
这创建了一个从48 - > -36开始的迭代
#5
0
u should have combined these two lines of code together, using this instead.
你应该将这两行代码组合在一起,而不是使用它。
print sorted(ListB, key=int, reverse=True)
result:
结果:
[48, 46, 25, 24, 22, 13, 8, -9, -15, -36]
#1
8
You are printing the list sorted ascending:
您正在打印以升序排序的列表:
print sorted(ListB)
If you want it descending, put your print statement on the previous line (where you reverse it)
如果您希望它降序,请将您的print语句放在上一行(您将其反转)
print sorted(ListB, key=int, reverse=True)
Then remove your final print statement.
然后删除最终的打印声明。
Example:
例:
>>> ListB = [24, 13, -15, -36, 8, 22, 48, 25, 46, -9]
>>> print sorted(ListB, key=int, reverse=True)
[48, 46, 25, 24, 22, 13, 8, -9, -15, -36]
#2
10
Try this, it'll sort the list in-place in descending order (there's no need to specify a key in this case):
试试这个,它会按降序排列列表(在这种情况下不需要指定键):
listB = [24, 13, -15, -36, 8, 22, 48, 25, 46, -9]
listB.sort(reverse=True) # listB gets modified
print listB
=> [48, 46, 25, 24, 22, 13, 8, -9, -15, -36]
Alternatively, you can create a new sorted list:
或者,您可以创建新的排序列表:
listB = [24, 13, -15, -36, 8, 22, 48, 25, 46, -9]
listC = sorted(listB, reverse=True) # listB remains untouched
print listC
=> [48, 46, 25, 24, 22, 13, 8, -9, -15, -36]
#3
2
ListB = [24, 13, -15, -36, 8, 22, 48, 25, 46, -9]
ListB = sorted(ListB, key=int, reverse=True)
print ListB
Sorted does not change the variable passed to it. So if you want to do anything with them you have to store sorted output into a variable.
Sorted不会更改传递给它的变量。因此,如果您想对它们执行任何操作,则必须将已排序的输出存储到变量中。
#4
0
reversed(sorted(listb))
This creates an iterable going from 48 -> -36
这创建了一个从48 - > -36开始的迭代
#5
0
u should have combined these two lines of code together, using this instead.
你应该将这两行代码组合在一起,而不是使用它。
print sorted(ListB, key=int, reverse=True)
result:
结果:
[48, 46, 25, 24, 22, 13, 8, -9, -15, -36]