[英]How to sort a text file into a list consisting of tuples and then appending to the list in python
[英]how to sort a list in python consisting of monitor resolutions?
我想對這個列表進行排序,但找不到方法。
我/你:
['1152x864', '1920x1080', '1600x900', '1280x1024', '1024x768', '640x480', '720x400', '800x600']
o / p:
['1920x1080', '1600x900', '1280x1024', '1152x864', '1024x768', '800x600', '720x400', '640x480']
提前致謝。
只需使用sorted
的key
參數:
res = sorted(data, key=lambda x: [int(s) for s in x.split("x")], reverse=True)
print(res)
Output
['1920x1080', '1600x900', '1280x1024', '1152x864', '1024x768', '800x600', '720x400', '640x480']
這將按高度打破聯系
您可以使用自定義 function 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)
output:
['1920x1080',
'1600x900',
'1280x1024',
'1152x864',
'1024x768',
'800x600',
'640x480',
'720x400']
我的最后一種方法似乎與您的案例最相關。 當您看到標有“14 英寸屏幕”的設備時,他們指的是對角線而不是高度或寬度。 所以,如果你想說 MacBook 15 > MacBook 13,那就是 go。 如果您純粹想使用分辨率和像素,請嘗試倒數第二個更相關的。
您可以使用sorted()
以及帶有 lambda function 的自定義鍵來提取高度、寬度並應用您的計算。 根據您要執行的操作,您還需要reverse=True
。
lambda function 將每個元素作為x
,然后分別使用x.split('x')[0]
和x.split('x')[1]
提取寬度和/或高度。 然后你可以選擇使用畢達哥拉斯定理來獲得對角線或者只是乘積它們來獲得你的像素等。
我盡量不為此使用任何外部庫,例如 numpy 或數學。 在此處閱讀有關已sorted
function 的更多信息。
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']
您想根據什么標准進行排序?
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']
按總像素數:
import math
sorted(l, key=lambda dim: math.prod(map(int, dim.split('x'))), reverse=True)
# ['1920x1080', '1600x900', '1280x1024', '1152x864', '1024x768', '800x600', '640x480', '720x400']
按對角線長度:
import math
sorted(l, key=lambda dim: math.hypot(*map(int, dim.split('x'))), reverse=True)
# ['1920x1080', '1600x900', '1280x1024', '1152x864', '1024x768', '800x600', '720x400', '640x480']
相關文件:
使用適當的排序指標,您可以將其實現為簡單的 function 並將其作為key
參數傳遞給sort
,例如:
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
你可以使用這個 function 它會起作用。
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
如果你想按照你想要的方式排序 output 你可以試試這個:
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))
但是如果你想按屏幕中的總像素數排序,你必須使用 go :
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)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.