gpt4 book ai didi

Python 按自定义排序 {key :order} pair

转载 作者:太空宇宙 更新时间:2023-11-03 15:02:50 25 4
gpt4 key购买 nike

我目前的代码是,给定一个列表,通过选择器中的正则表达式(用户定义)拆分名称,并按键(位置 - 也是用户定义)排序。

可以是按其排序顺序排列的键列表。

例如:

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']
}

基本上,这将排序键 0FF TT SS排序然后排序键 3SS 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)

需要有关forced_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"]

最佳答案

我将您在这里尝试做的事情称为:类别排序。您传入的字典显示如何对某些字段进行排序。但是你的字典没有,因为它不能显示如何对字段进行排序。 Python 字典的键是无序的。我建议保留关键字段的含义,无论您是否传入显示类别排序的 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

关于Python 按自定义排序 {key :order} pair,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44918876/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com