目录
问题描述:
['kw_85
-1_Avg', 'kw_85
-2_Avg', 'kw_70-1_Avg', 'kw_70-2_Avg', 'kw_50-3_Avg', 'kw_10-4_Avg']
将第一个数字重复的字符串(如:'kw_85
-1_Avg', 'kw_85
-2_Avg'),
删除一个,保留第二个数字最小的字符串(如:kw_85
-1_Avg)
python
def sort_column(columns):
# 提取第一个数字部分,并存储到字典中
first_digit_dict = {}
for col in columns:
match = re.match(r'ws_(\d+)-(\d+)_Avg', col)
if match:
key = match.group(1) # 提取第一个数字部分
if key not in first_digit_dict:
first_digit_dict[key] = col
else:
# 如果有多个相同的key,选择优先级更高的列
current_priority = int(first_digit_dict[key].split('-')[1].split('_')[0])
new_priority = int(col.split('-')[1].split('_')[0])
if new_priority < current_priority:
first_digit_dict[key] = col
unique_columns = list(first_digit_dict.values())
return unique_columns