用 space 分隔的整数字符串对列表进行排序
Sort a list with string of integers separated by space
Python:
我有一个具有以下结构的一维列表(每个条目都是一串由 space 分隔的整数):
vals = ["121",
"121 122",
"122",
"122 124",
"150",
"171",
"49",
"49 122",
"49 122" "124",
"49 122" "516",
"51",
"51 122",
"516",
"8",
"8 122" "516",
"8 124",
"8 171",
"8 49",
"8 49" "124",
"8 49" "516",
"8 51",
"8 51" "122",
"8 516",
"878",
]
但我正在寻找关于行和列顺序中每个整数的排序顺序,即预期输出是
vals = [
"8",
"8 49",
"8 49 124",
"8 49 516",
"8 51",
"8 51 122",
"49",
"49 122",
"49 122 124",
"49 122 516",
"51",
"51 122",
"121",
"121 122",
"122",
"122 124",
"150",
"171",
"516",
"878",
]
我已经尝试使用 sort()
和 sorted()
机制,但似乎没有任何效果
sort_len = list1.sort(key = int)
--> 这是抛出值错误,因为每个列表都是一串分隔的字母。
我想按行值对列表进行排序,即一行中的第一个整数(如果行值相同,即第一个整数相同,则转到下一列)
您可以按每行的最小值排序
list1.sort(key=lambda e:min([int(se) for se in e.split()]))
诀窍是将每个字符串转换为整数元组。
print(sorted(vals, key = lambda y: tuple(int(x) for x in y.split())))
['8', '8 49', '8 49 124', '8 49 516', '8 51', '8 51 122', '8 122 516', '8 124', '8 171', '8 516', '49', '49 122', '49 122 124', '49 122 516', '51', '51 122', '121', '121 122', '122', '122 124', '150', '171', '516', '878']
您可以使用 sorted 函数,也可以使用其中的 key 参数根据您的自定义值对值进行排序 way.I 已经为 this.Have 编写了代码,看看这个:
lis =["121","51 122","122 123 23","2 13 4"]
newlis =[]
for i in lis:
numbers = [int(s) for s in i.split(" ")]
numbers = [str(s) for s in sorted(numbers)]
newlis.append(str(" ".join(numbers)))
newlis = sorted(newlis,key = lambda x: [int(s) for s in x.split(" ")][0])
print(newlis)
您首先可以做的是使用
将每个字符串元素分成一个整数列表
[list(map(int, i.split())) for i in vals]
然后按从最后到第一的值的每个索引对列表进行排序。假设长度最多可以为3,
func = lambda x,p: x[len(x)-p if len(x)-p > 0 else 0]
vals = sorted(vals, key=lambda x: func(x,1))
vals = sorted(vals, key=lambda x: func(x,2))
vals = sorted(vals, key=lambda x: func(x,3))
然后你的值将被排序。例如,如果你 运行 你的示例变量通过这个,输出将是
[[8], [8, 49], [8, 51], [8, 124], [8, 171], [8, 516], [8, 49124], [8, 49516], [8, 51122], [8, 122516], [49], [49, 122], [49, 122124], [49, 122516], [51], [51, 122], [121], [121, 122], [122], [122, 124], [150], [171], [516], [878]]
之后,只需使用
将其转换回字符串
vals = list(map(lambda x:" ".join([str(i) for i in x]), vals))
作为函数的完整程序:
def sort_vals(vals):
vals = [list(map(int, i.split())) for i in vals]
func = lambda x,p: x[len(x)-p if len(x)-p > 0 else 0]
vals = sorted(vals, key=lambda x: func(x,1))
vals = sorted(vals, key=lambda x: func(x,2))
vals = sorted(vals, key=lambda x: func(x,3))
vals = list(map(lambda x:" ".join([str(i) for i in x]), vals))
return vals
列和行的分离排序。没有 for 循环更像 pythonic
代码:
def sort_str(in_str):
in_lst = [int(s) for s in in_str.split()]
return ' '.join(str(i) for i in sorted(in_lst))
lst = ['121', '121 122', '122', '122 124', '150', '171', '49', '49 122', '49 122 124', '49 122 516', '51', '51 122', '516', '8', '8 122 516', '8 124', '8 171', '8 49', '8 49 124', '8 49 516', '8 51', '8 51 122', '8 516', '878' ]
lst = [sort_str(ele) for ele in lst]
print(lst) # Sorted columns:
lst.sort(key = lambda s : int(s.split()[0]))
print(lst) #Sorted rows
输出:
排序列:
['121', '121 122', '122', '122 124', '150', '171', '49', '49 122', '49 122 124', '49 122 516', '51', '51 122', '516', '8', '8 122 516', '8 124', '8 171', '8 49', '8 49 124', '8 49 516', '8 51', '8 51 122', '8 516', '878']
已排序的行:
['8', '8 122 516', '8 124', '8 171', '8 49', '8 49 124', '8 49 516', '8 51', '8 51 122', '8 516', '49', '49 122', '49 122 124', '49 122 516', '51', '51 122', '121', '121 122', '122', '122 124', '150', '171', '516', '878']
Python: 我有一个具有以下结构的一维列表(每个条目都是一串由 space 分隔的整数):
vals = ["121",
"121 122",
"122",
"122 124",
"150",
"171",
"49",
"49 122",
"49 122" "124",
"49 122" "516",
"51",
"51 122",
"516",
"8",
"8 122" "516",
"8 124",
"8 171",
"8 49",
"8 49" "124",
"8 49" "516",
"8 51",
"8 51" "122",
"8 516",
"878",
]
但我正在寻找关于行和列顺序中每个整数的排序顺序,即预期输出是
vals = [
"8",
"8 49",
"8 49 124",
"8 49 516",
"8 51",
"8 51 122",
"49",
"49 122",
"49 122 124",
"49 122 516",
"51",
"51 122",
"121",
"121 122",
"122",
"122 124",
"150",
"171",
"516",
"878",
]
我已经尝试使用 sort()
和 sorted()
机制,但似乎没有任何效果
sort_len = list1.sort(key = int)
--> 这是抛出值错误,因为每个列表都是一串分隔的字母。
我想按行值对列表进行排序,即一行中的第一个整数(如果行值相同,即第一个整数相同,则转到下一列)
您可以按每行的最小值排序
list1.sort(key=lambda e:min([int(se) for se in e.split()]))
诀窍是将每个字符串转换为整数元组。
print(sorted(vals, key = lambda y: tuple(int(x) for x in y.split())))
['8', '8 49', '8 49 124', '8 49 516', '8 51', '8 51 122', '8 122 516', '8 124', '8 171', '8 516', '49', '49 122', '49 122 124', '49 122 516', '51', '51 122', '121', '121 122', '122', '122 124', '150', '171', '516', '878']
您可以使用 sorted 函数,也可以使用其中的 key 参数根据您的自定义值对值进行排序 way.I 已经为 this.Have 编写了代码,看看这个:
lis =["121","51 122","122 123 23","2 13 4"]
newlis =[]
for i in lis:
numbers = [int(s) for s in i.split(" ")]
numbers = [str(s) for s in sorted(numbers)]
newlis.append(str(" ".join(numbers)))
newlis = sorted(newlis,key = lambda x: [int(s) for s in x.split(" ")][0])
print(newlis)
您首先可以做的是使用
[list(map(int, i.split())) for i in vals]
然后按从最后到第一的值的每个索引对列表进行排序。假设长度最多可以为3,
func = lambda x,p: x[len(x)-p if len(x)-p > 0 else 0]
vals = sorted(vals, key=lambda x: func(x,1))
vals = sorted(vals, key=lambda x: func(x,2))
vals = sorted(vals, key=lambda x: func(x,3))
然后你的值将被排序。例如,如果你 运行 你的示例变量通过这个,输出将是
[[8], [8, 49], [8, 51], [8, 124], [8, 171], [8, 516], [8, 49124], [8, 49516], [8, 51122], [8, 122516], [49], [49, 122], [49, 122124], [49, 122516], [51], [51, 122], [121], [121, 122], [122], [122, 124], [150], [171], [516], [878]]
之后,只需使用
将其转换回字符串vals = list(map(lambda x:" ".join([str(i) for i in x]), vals))
作为函数的完整程序:
def sort_vals(vals):
vals = [list(map(int, i.split())) for i in vals]
func = lambda x,p: x[len(x)-p if len(x)-p > 0 else 0]
vals = sorted(vals, key=lambda x: func(x,1))
vals = sorted(vals, key=lambda x: func(x,2))
vals = sorted(vals, key=lambda x: func(x,3))
vals = list(map(lambda x:" ".join([str(i) for i in x]), vals))
return vals
列和行的分离排序。没有 for 循环更像 pythonic
代码:
def sort_str(in_str):
in_lst = [int(s) for s in in_str.split()]
return ' '.join(str(i) for i in sorted(in_lst))
lst = ['121', '121 122', '122', '122 124', '150', '171', '49', '49 122', '49 122 124', '49 122 516', '51', '51 122', '516', '8', '8 122 516', '8 124', '8 171', '8 49', '8 49 124', '8 49 516', '8 51', '8 51 122', '8 516', '878' ]
lst = [sort_str(ele) for ele in lst]
print(lst) # Sorted columns:
lst.sort(key = lambda s : int(s.split()[0]))
print(lst) #Sorted rows
输出:
排序列:
['121', '121 122', '122', '122 124', '150', '171', '49', '49 122', '49 122 124', '49 122 516', '51', '51 122', '516', '8', '8 122 516', '8 124', '8 171', '8 49', '8 49 124', '8 49 516', '8 51', '8 51 122', '8 516', '878']
已排序的行:
['8', '8 122 516', '8 124', '8 171', '8 49', '8 49 124', '8 49 516', '8 51', '8 51 122', '8 516', '49', '49 122', '49 122 124', '49 122 516', '51', '51 122', '121', '121 122', '122', '122 124', '150', '171', '516', '878']