📜  Python – 按自定义整数子字符串对字符串进行排序

📅  最后修改于: 2022-05-13 01:55:20.813000             🧑  作者: Mango

Python – 按自定义整数子字符串对字符串进行排序

给定一个字符串列表,根据列表中子字符串的出现对字符串进行排序。

方法 #1:使用 sorted() + zip() + lambda + regex()

上述功能的组合可以用来解决这个问题。在此,我们使用 regex() 和 sorted() 执行子字符串排序任务, zip() 用于产生最终结果。

Python3
# Python3 code to demonstrate working of
# Sort String by Custom Substrings
# Using sorted() + zip() + lambda + regex()
import re
 
# initializing list
test_list = ["Good at 4", "Wake at 7", "Work till 6", "Sleep at 11"]
 
# printing original list
print("The original list : " + str(test_list))
 
# initializing substring list
subord_list = ["6", "7", "4", "11"]
 
 
# creating inverse mapping with index
temp_dict = {val: key for key, val in enumerate(subord_list)}
 
# custom sorting
temp_list = sorted([[ele, temp_dict[re.search("(\d+)$", ele).group()]] \
                for ele in test_list], key = lambda x: x[1])
# compiling result
res = [ele for ele in list(zip(*temp_list))[0]]
         
# printing result
print("The sorted list : " + str(res))


Python3
# Python3 code to demonstrate working of
# Sort String by Custom Substrings
# Using sorted() + comparator + regex()
import re
 
# helper function to solve problem
def hlper_fnc(ele):
    temp = re.search("(\d+)$", ele).group()
    return temp_dict[temp] if temp in temp_dict else int(temp)
 
# initializing list
test_list = ["Good at 4", "Wake at 7", "Work till 6", "Sleep at 11"]
 
# printing original list
print("The original list : " + str(test_list))
 
# initializing substring list
subord_list = ["6", "7", "4", "11"]
 
# creating inverse mapping with index
temp_dict = {val: key for key, val in enumerate(test_list)}
 
# sorting using comparator
test_list.sort(key = lambda ele: hlper_fnc(ele))
         
# printing result
print("The sorted list : " + str(test_list))


输出
The original list : ['Good at 4', 'Wake at 7', 'Work till 6', 'Sleep at 11']
The sorted list : ['Work till 6', 'Wake at 7', 'Good at 4', 'Sleep at 11']

方法 #2:使用 sorted() + zip() + 比较器 + regex()

这是可以执行此任务的另一种方式。在与上述方法使用类似的功能中,不同之处在于比较器函数被馈送到 sorted() 而不是 lambda 进行排序。

Python3

# Python3 code to demonstrate working of
# Sort String by Custom Substrings
# Using sorted() + comparator + regex()
import re
 
# helper function to solve problem
def hlper_fnc(ele):
    temp = re.search("(\d+)$", ele).group()
    return temp_dict[temp] if temp in temp_dict else int(temp)
 
# initializing list
test_list = ["Good at 4", "Wake at 7", "Work till 6", "Sleep at 11"]
 
# printing original list
print("The original list : " + str(test_list))
 
# initializing substring list
subord_list = ["6", "7", "4", "11"]
 
# creating inverse mapping with index
temp_dict = {val: key for key, val in enumerate(test_list)}
 
# sorting using comparator
test_list.sort(key = lambda ele: hlper_fnc(ele))
         
# printing result
print("The sorted list : " + str(test_list))
输出
The original list : ['Good at 4', 'Wake at 7', 'Work till 6', 'Sleep at 11']
The sorted list : ['Good at 4', 'Work till 6', 'Wake at 7', 'Sleep at 11']