Python 按嵌套列表的值对列表进行排序
只有当我有3位数的嵌套列表时,我才知道如何对列表进行排序。路在这里Python 按嵌套列表的值对列表进行排序,python,list,sorting,Python,List,Sorting,只有当我有3位数的嵌套列表时,我才知道如何对列表进行排序。路在这里 from colorthief import ColorThief from operator import itemgetter import itertools import sys import os import glob import math import hilbert folder = sys.argv[1] imgfiles = glob.glob(os.path.join(folder, "*.jp
from colorthief import ColorThief
from operator import itemgetter
import itertools
import sys
import os
import glob
import math
import hilbert
folder = sys.argv[1]
imgfiles = glob.glob(os.path.join(folder, "*.jpg"))
dominant_rgb_color = []
files_domColor = []
for imgfile in imgfiles:
color_thief = ColorThief(imgfile)
dominant_rgb_color.append(list(color_thief.get_color(quality=1)))
for a, b in itertools.izip(imgfiles, dominant_rgb_color):
files_domColor.append([a,b])
files_domColor.sort(key=lambda(r,g,b):hilbert.Hilbert_to_int([int(r*255),int(g*255),int(b*255)]))
for f in files_domColor:
print f
但是使用这3位数字连接文件是很有用的,所以我使用izip函数将它们放在列表中。但是现在是否可以将列表与我添加的第一个元素一起再次排序?文件颜色列表的输出为
sort(key=lambda(r,g,b):hilbert.Hilbert_to_int([int(r*255),int(g*255),int(b*255)]))
所以,我想对所有列表进行排序。因此,问题是我如何对每个列表的[0]元素进行排序,因为我只知道如何对[1]元素进行排序?你的意思是,像这样吗
[['temp2\\11.jpg', [16, 17, 14]], ['temp2\\7.jpg', [40, 33, 27]], ['temp2\\8.jpg', [31, 26, 22]], ['temp2\\9.jpg', [39, 32, 25]]]
你能举例说明吗?当然可以。。。。使用这个代码sortkey=lambdar,g,b:hilbert.hilbert_to_int[intr*255,intg*255,intb*255]我可以对一个列表进行排序,比如[16,17,14],[40,33,27],[31,26,22],[31,26,22],[temp2\\11.jpg',[16,17,14],[temp2\\7.jpg',[40,33,27],[temp2\\8.jpg',[31,26,22]。]我相信您导入itemgetter的过程是正确的,但我没有看到它在您的代码中使用。您研究过吗?更具体地说,排序函数将颜色从较暖到较冷进行排序,它只需要一个包含3位数字的列表就可以工作。我只是为了组合我所拥有的jpg文件,将每张照片的路径添加到嵌套列表中。我确实这样做了我不知道这是否是个好习惯。
sort(key=lambda(x):hilbert.Hilbert_to_int([int(x[1][0]*255),int(x[1][1]*255),int(x[1][2]*255)]))