问题描述
我目前有一些代码,给定一个列表,该代码在选择器中由正则表达式对名称进行拆分(用户定义),并按键进行排序(位置-用户定义).
I currently have code that, given a list, splits the names by the regexp in the selector (user defined) and sorts by the key (location - also user defined).
键可以是按键排序顺序的键列表.
The key can be a list of keys in the order in which they should be sorted.
例如:
selector = r'.*(FF|TT|SS)_([-\.\d]+v)_([-\.\d]+c)_(FF|TT|SS).*'
key = [2,1,3]
将按照温度,电压,次级过程进行排序.
Would sort by temp, voltage, secondary process.
该部分代码可以完美运行.现在,我需要帮助来弄清楚如何给定自定义{key:order}配对字典.例如:
That part of the code works perfectly. Now, I need help figuring out how to sort if given a custom {key:order} paired dictionary. For example:
{
0: ['FF', 'TT', 'SS'],
3: ['SS', 'TT', 'FF']
}
基本上,这将按FF TT SS
顺序对键0
进行排序,然后对SS TT FF
进行键3
排序.
Basically, this would sort key 0
with FF TT SS
order and then sort key 3
with SS TT FF
.
这是我到目前为止所拥有的:
Here's what I have so far:
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)
在"forced_order"部分需要帮助.这是我的输入:
Need help with the forced_order section. Here's my input:
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部分的预期输出:
Expected output for the forced_order part:
["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"]
推荐答案
在这里,我会称呼您正在尝试做的事情:类别订购.您要传递的字典显示了如何对某些字段进行排序.但是您的字典不能显示的(因为它不能显示)是如何对字段进行排序. Python字典在键上是无序的.无论您是否通过dict
(显示类别顺序),我都建议保留键字段的含义.
I would call what you are trying to do here: Category Ordering. The dictionary you are passing in shows how to order certain fields. But what your dictionary does not, because it can not, show is how to order the fields. Python dicts are un-ordered on the keys. I would suggest keeping the meaning of the key field regardless of whether or not you pass in the dict
which shows the category ordering.
关于如何利用category_order
中的信息,我认为您需要将convert()
例程更改为以下内容:
As to how to make use of the information in the category_order
, I think you need to change the convert()
routine to something like:
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
这篇关于Python通过自定义{key:order}对进行排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!