我目前有一些代码,给定一个列表,用选择器中的regexp(用户定义)拆分名称,然后按键排序(位置 - 也是用户定义的)。
键可以是按顺序排列的键列表。
例如:
selector = r'.*(FF|TT|SS)_([-\.\d]+v)_([-\.\d]+c)_(FF|TT|SS).*'
key = [2,1,3]
按温度,电压,二次加工排序。
这部分代码完美无缺。现在,如果给出一个自定义的{key:order}配对词典,我需要帮助弄清楚如何排序。例如:
{
0: ['FF', 'TT', 'SS'],
3: ['SS', 'TT', 'FF']
}
基本上,这会将键0
与FF TT SS
顺序排序,然后使用3
对键SS TT FF
进行排序。
这是我到目前为止所拥有的:
import re
def sort_names(format_ids, selector, key=1, forced_order=None):
if isinstance(key, int):
key = [key]
def convert(x):
try:
return float(x[:-1])
except ValueError:
return x
def sort_keys(ik):
def split_fid(x):
x = re.findall(selector,x)[0]
return tuple([convert(x[i]) for i in ik])
return split_fid
if forced_order:
format_ids.sort(key=sort_keys(forced_order.keys()))
return list(format_ids)
else:
format_ids.sort(key=sort_keys(key))
return list(format_ids)
需要force_order部分的帮助。这是我的意见:
fids = ["synopsys_SS_2v_-40c_FF.lib",
"synopsys_SS_1v_-40c_TT.lib",
"synopsys_SS_1.2v_-40c_SS.lib",
"synopsys_SS_1.4v_-40c_SS.lib",
"synopsys_SS_2v_-40c_TT.lib",
"synopsys_FF_3v_25c_FF.lib",
"synopsys_TT_4v_125c_TT.lib",
"synopsys_TT_1v_85c_TT.lib",
"synopsys_TT_10v_85c_TT.lib",
"synopsys_FF_3v_-40c_SS.lib",
"synopsys_FF_3v_-40c_TT.lib"]
se = r'.*(FF|TT|SS)_([-\.\d]+v)_([-\.\d]+c)_(FF|TT|SS).*'
k = 3
fo = {
0: ['FF', 'TT', 'SS'],
3: ['SS', 'TT', 'FF']
}
retlist = sort_names(fids, se, k, fo)
forced_order部分的预期输出:
["synopsys_FF_3v_-40c_SS.lib",
"synopsys_FF_3v_-40c_TT.lib",
"synopsys_FF_3v_25c_FF.lib",
"synopsys_TT_4v_125c_TT.lib",
"synopsys_TT_1v_85c_TT.lib",
"synopsys_TT_10v_85c_TT.lib",
"synopsys_SS_1.2v_-40c_SS.lib",
"synopsys_SS_1.4v_-40c_SS.lib",
"synopsys_SS_1v_-40c_TT.lib",
"synopsys_SS_2v_-40c_TT.lib",
"synopsys_SS_2v_-40c_FF.lib"]
答案 0 :(得分:1)
我会打电话给你在这里做的事情:分类订购。您传入的字典显示了如何订购某些字段。但是你的字典没有,因为它不能,显示如何订购字段。 Python dicts在键上没有排序。无论您是否传递显示类别排序的dict
,我都建议保留关键字段的含义。
至于如何使用category_order
中的信息,我认为您需要将convert()
例程更改为:
def convert(i, x):
if i in category_order:
return category_order[i].index(x)
try:
return float(x[:-1])
except ValueError:
return x
return tuple([convert(i, fields[i]) for i in key])
def sort_names(format_ids, selector, key=1, category_order=None):
if isinstance(key, int):
key = [key]
if category_order is None:
category_order = {}
SELECTOR_RE = re.compile(selector)
def convert(i, x):
if i in category_order:
return category_order[i].index(x)
try:
return float(x[:-1])
except ValueError:
return x
def sort_keys():
def split_fid(fid):
fields = SELECTOR_RE.findall(fid)[0]
return tuple([convert(i, fields[i]) for i in key])
return split_fid
result = list(format_ids)
result.sort(key=sort_keys())
return result
fids = ["synopsys_SS_2v_-40c_FF.lib",
"synopsys_SS_1v_-40c_TT.lib",
"synopsys_SS_1.2v_-40c_SS.lib",
"synopsys_SS_1.4v_-40c_SS.lib",
"synopsys_SS_2v_-40c_TT.lib",
"synopsys_FF_3v_25c_FF.lib",
"synopsys_TT_4v_125c_TT.lib",
"synopsys_TT_1v_85c_TT.lib",
"synopsys_TT_10v_85c_TT.lib",
"synopsys_FF_3v_-40c_SS.lib",
"synopsys_FF_3v_-40c_TT.lib"]
se = r'.*(FF|TT|SS)_([-\.\d]+v)_([-\.\d]+c)_(FF|TT|SS).*'
k = [0, 3]
fo = {
0: ['FF', 'TT', 'SS'],
3: ['SS', 'TT', 'FF']
}
print('\n'.join(sort_names(fids, se, k, fo)))
synopsys_FF_3v_-40c_SS.lib
synopsys_FF_3v_-40c_TT.lib
synopsys_FF_3v_25c_FF.lib
synopsys_TT_4v_125c_TT.lib
synopsys_TT_1v_85c_TT.lib
synopsys_TT_10v_85c_TT.lib
synopsys_SS_1.2v_-40c_SS.lib
synopsys_SS_1.4v_-40c_SS.lib
synopsys_SS_1v_-40c_TT.lib
synopsys_SS_2v_-40c_TT.lib
synopsys_SS_2v_-40c_FF.lib
print('\n'.join(sort_names(fids, se, [2, 1, 3], fo)))
给出:
synopsys_SS_1v_-40c_TT.lib
synopsys_SS_1.2v_-40c_SS.lib
synopsys_SS_1.4v_-40c_SS.lib
synopsys_SS_2v_-40c_TT.lib
synopsys_SS_2v_-40c_FF.lib
synopsys_FF_3v_-40c_SS.lib
synopsys_FF_3v_-40c_TT.lib
synopsys_FF_3v_25c_FF.lib
synopsys_TT_1v_85c_TT.lib
synopsys_TT_10v_85c_TT.lib
synopsys_TT_4v_125c_TT.lib