我想对此列表进行排序,但找不到方法。
我/p:
['1152x864', '1920x1080', '1600x900', '1280x1024', '1024x768', '640x480', '720x400', '800x600']
o/p :
['1920x1080', '1600x900', '1280x1024', '1152x864', '1024x768', '800x600', '720x400', '640x480']
提前致谢。
我的最后一种方法似乎与您的案例最相关。当您看到标有“14 英寸屏幕”的设备时,他们指的是对角线,而不是高度或宽度。因此,如果您想说 MacBook 15 > MacBook 13,那么这就是您的最佳选择。如果您纯粹想使用分辨率和像素,请尝试倒数第二个,这会更相关。
您可以使用
sorted()
以及带有 lambda 函数的自定义键来提取高度、宽度并应用您的计算。根据您想要做的事情,您还需要 reverse=True
。
lambda 函数将每个元素视为
x
,然后分别使用 x.split('x')[0]
和 x.split('x')[1]
提取宽度和/或高度。然后您可以选择使用毕达哥拉斯定理来获得对角线或仅将它们相乘以获得像素等..
我尝试不使用任何外部库,例如 numpy 或 math 。阅读有关
sorted
函数的更多信息这里。
sorted(l, key=lambda x: int(x.split('x')[0]), reverse=True)
sorted(l, key=lambda x: int(x.split('x')[1]), reverse=True)
sorted(l, key=lambda x: int(x.split('x')[0])/int(x.split('x')[1]), reverse=True)
sorted(l, key=lambda x: int(x.split('x')[0])*int(x.split('x')[1]), reverse=True)
屏幕尺寸 = sqrt((宽度^2)+(高度^2))
sorted(l, key=lambda x: (int(x.split('x')[0])**2+int(x.split('x')[1])**2)**(1/2), reverse=True)
['1920x1080',
'1600x900',
'1280x1024',
'1152x864',
'1024x768',
'800x600',
'720x400',
'640x480']
只需使用
key
的
sorted
参数即可:
res = sorted(data, key=lambda x: [int(s) for s in x.split("x")], reverse=True)
print(res)
输出
['1920x1080', '1600x900', '1280x1024', '1152x864', '1024x768', '800x600', '720x400', '640x480']
这将打破身高的束缚
您可以将
sorted
与自定义函数一起使用,这里我取两个维度的乘积(即总像素大小):
l = ['1152x864', '1920x1080', '1600x900', '1280x1024', '1024x768', '640x480', '720x400', '800x600']
from math import prod
sorted(l, key=lambda x: prod(map(int, x.split('x'))), reverse=True)
输出:
['1920x1080',
'1600x900',
'1280x1024',
'1152x864',
'1024x768',
'800x600',
'640x480',
'720x400']
使用适当的排序指标,您可以将其实现为一个简单的函数,并将其作为
sort
参数传递给 key
,例如:
def key(res):
return (*map(int, s.split("x")))
resolutions.sort(key=key, reverse=True)
这将首先按宽度排序,然后按高度排序。或者更一般地说:
def key(res):
width, height = map(int, s.split("x"))
result = # ... any numeric value calculated from the two
return result
您需要决定要按照什么标准进行排序。
l = ['1152x864', '1920x1080', '1600x900', '1280x1024', '1024x768', '640x480', '720x400', '800x600']
按宽度:
sorted(l, key=lambda dim: int(dim.split('x')[0]), reverse=True)
# ['1920x1080', '1600x900', '1280x1024', '1152x864', '1024x768', '800x600', '720x400', '640x480']
按身高:
sorted(l, key=lambda dim: int(dim.split('x')[1]), reverse=True)
# ['1920x1080', '1280x1024', '1600x900', '1152x864', '1024x768', '800x600', '640x480', '720x400']
按总像素数:
from math import prod
sorted(l, key=lambda dim: prod(map(int, dim.split('x'))), reverse=True)
# ['1920x1080', '1600x900', '1280x1024', '1152x864', '1024x768', '800x600', '640x480', '720x400']
按对角线长度:
from math import hypot
sorted(l, key=lambda dim: hypot(*map(int, dim.split('x'))), reverse=True)
# ['1920x1080', '1600x900', '1280x1024', '1152x864', '1024x768', '800x600', '720x400', '640x480']
相关文档:
你可以使用这个功能,它会起到作用。
def orderByResolution(arr):
mytempdict = {}
result = []
for i in arr:
mykey = int(i[:i.index('x')])
mytempdict[mykey] = i
for i in reversed(sorted(mytempdict)):
result.append(mytempdict[i])
return result
如果您想按照想要的输出进行排序,您可以尝试以下操作:
def bubbleSort(arr):
arr = arr.copy()
n = len(arr)
# Traverse through all array elements
for i in range(n-1):
# range(n) also work but outer loop will repeat one time more than needed.
# Last i elements are already in place
for j in range(0, n-i-1):
# traverse the array from 0 to n-i-1
# Swap if the element found is greater
# than the next element
if int(arr[j].split("x")[0]) < int(arr[j + 1].split("x")[0]) :
arr[j], arr[j + 1] = arr[j + 1], arr[j]
return arr
if __name__ == "__main__":
inp_reso = ['1152x864', '1920x1080', '1600x900', '1280x1024', '1024x768', '640x480', '720x400', '800x600']
print(bubbleSort(inp_reso))
但是如果您想按屏幕中的总像素数排序,则必须使用:
def bubbleSort(arr):
arr = arr.copy()
n = len(arr)
# Traverse through all array elements
for i in range(n-1):
# range(n) also work but outer loop will repeat one time more than needed.
# Last i elements are already in place
for j in range(0, n-i-1):
# traverse the array from 0 to n-i-1
# Swap if the element found is greater
# than the next element
if eval(arr[j].replace("x", "*")) < eval(arr[j + 1].replace("x", "*")) :
arr[j], arr[j + 1] = arr[j + 1], arr[j]
return arr
if __name__ == "__main__":
inp_reso = ['1152x864', '1920x1080', '1600x900', '1280x1024', '1024x768', '640x480', '720x400', '800x600']
print(bubbleSort(inp_reso))
l=['1152x864', '1920x1080', '1600x900', '1280x1024', '1024x768', '640x480', '720x400', '800x600']
for i in range(len(l)):
l[i]=float(l[i].replace ('x','.'))
l.sort(reverse=True)
for i in range(len(l)):
l[i]=str(l[i]).replace ('.','x')
print(l)