import glob
import os
print([entry for entry in glob.iglob("{}/**".format("DIRECTORY_PATH"), recursive=True) if os.path.isfile(entry) == True])
其中obj1.serial是234v5,obj2.serial是lobf56和obj3.serial是123b4
a = ['123b4', '234v5', 'lobf56']
b = [obj1, obj2, obj3] # where each obj is list of object which has attribute called 'serial' which matches serial numbers in list #a
输出:[obj3,obj1,obj2]
我目前能够以上述方式实现排序。但是有更好的方法吗?
答案 0 :(得分:1)
list comprehension有帮助吗?
[obj for each in a for obj in b if each == obj.serial]
如果您将两者之间的时间进行比较,您的方法将花费:
1.6 µs ± 25.5 ns per loop (mean ± std. dev. of 7 runs, 1000000 loops each)
列表理解需要:
1.37 µs ± 18.2 ns per loop (mean ± std. dev. of 7 runs, 1000000 loops each)
因此,如果用“一种更好的方法”表示效率。这绝对重要。