I want to sort this list but couldn't find a way to do it.
i/p :
['1152x864', '1920x1080', '1600x900', '1280x1024', '1024x768', '640x480', '720x400', '800x600']
o/p :
['1920x1080', '1600x900', '1280x1024', '1152x864', '1024x768', '800x600', '720x400', '640x480']
Thanks in advance.
CodePudding user response:
sorted(l, key=lambda x: int(x.split('x')[0]), reverse=True)
Sorting by height only -
sorted(l, key=lambda x: int(x.split('x')[1]), reverse=True)
Sorting by Aspect ratio (width/height) -
sorted(l, key=lambda x: int(x.split('x')[0])/int(x.split('x')[1]), reverse=True)
Sorting by the number of pixels (width*height) -
sorted(l, key=lambda x: int(x.split('x')[0])*int(x.split('x')[1]), reverse=True)
Sorting by monitor screen size (diagonal of the screen) -
screen size = sqrt((width^2) (height^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']
The last one seems to be the most relavant for your case.
CodePudding user response:
You can use sorted
with a custom function, here I am taking the product (i.e., total pixel size) of the two dimensions:
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)
output:
['1920x1080',
'1600x900',
'1280x1024',
'1152x864',
'1024x768',
'800x600',
'640x480',
'720x400']
CodePudding user response:
Use an appropriate sort metric that you can implement as a simple function and pass to sort
as key
parameter, e.g.:
def key(res):
return (*map(int, s.split("x")))
resolutions.sort(key=key, reverse=True)
This will sort by width first, and height second. Or more generally:
def key(res):
width, height = map(int, s.split("x"))
result = # ... any numeric value calculated from the two
return result
CodePudding user response:
if you want to sort as you want to get your desired output you can try this:
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))
but if you want to sort by number of total pixels in screen you must go with:
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))
CodePudding user response:
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)
CodePudding user response:
You can use this function it will do trick.
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