Python 未按预期排序
Python not sorting as expected
我正在尝试使用 python 对字母数字进行排序。但是我无法按照我想要的顺序得到它们。
printer.py
class Lease:
def __init__(self, renter=None, property=None, text=None):
self.renter = renter
self.property = property
self.text = text
lease_list = []
lines = open('input.txt', 'r')
for line in lines:
l, m = line.split(' - ')
l = re.sub(r"\D", "", l)
lease_list.append(Lease(m,l, line))
lines.close()
lease_list.sort(key=lambda obj: obj.property)
for lease in lease_list:
print lease.text
当前输出
#1B - Jackson
#1A - Brown
#100 - Rice
#50 - Smith
#8 - Smith
期望的输出
#1A - Brown
#1B - Jackson
#8 - Smith
#50 - Smith
#100 - Rice
input.txt
#50 - Smith
#8 - Smith
#100 - Rice
#1B - Jackson
#1A - Brown
您正在删除此处的文字
l = re.sub(r"\D", "", l)
这就是 属性 列表的样子
50
8
100
1
1
所以排序是正确的。问题是字符串排序和数字排序都不起作用。您必须先按数字排序,然后再按文字排序。这会起作用,但效率低下。您可能需要使用格式化数据创建一个临时数组并对其进行排序。
for line in lines:
l, m = line.split(' - ')
lease_list.append(Lease(m,l, line))
lease_list.sort(key=lambda obj: (int(re.sub(r"\D", "", obj.property)), re.sub(r"[^A-Z]+", "", obj.property)) )
for lease in lease_list:
print(lease.text)
只是提高效率的想法:
for line in lines:
l, m = line.split(' - ')
formatted_data.append({'property' : int(re.sub(r"\D", "", l))
, 'literal' : re.sub(r"[^A-Z]", "", l)
, 'm': m
, 'line': line})
formatted_data.sort(key=lambda obj: (obj['property'], obj['literal']))
for lease in formatted_data:
print(lease['line'])
for line in formatted_data:
lease_list.append(Lease(line['m'], line['property'], line['line']))
我正在尝试使用 python 对字母数字进行排序。但是我无法按照我想要的顺序得到它们。
printer.py
class Lease:
def __init__(self, renter=None, property=None, text=None):
self.renter = renter
self.property = property
self.text = text
lease_list = []
lines = open('input.txt', 'r')
for line in lines:
l, m = line.split(' - ')
l = re.sub(r"\D", "", l)
lease_list.append(Lease(m,l, line))
lines.close()
lease_list.sort(key=lambda obj: obj.property)
for lease in lease_list:
print lease.text
当前输出
#1B - Jackson
#1A - Brown
#100 - Rice
#50 - Smith
#8 - Smith
期望的输出
#1A - Brown
#1B - Jackson
#8 - Smith
#50 - Smith
#100 - Rice
input.txt
#50 - Smith
#8 - Smith
#100 - Rice
#1B - Jackson
#1A - Brown
您正在删除此处的文字
l = re.sub(r"\D", "", l)
这就是 属性 列表的样子
50
8
100
1
1
所以排序是正确的。问题是字符串排序和数字排序都不起作用。您必须先按数字排序,然后再按文字排序。这会起作用,但效率低下。您可能需要使用格式化数据创建一个临时数组并对其进行排序。
for line in lines:
l, m = line.split(' - ')
lease_list.append(Lease(m,l, line))
lease_list.sort(key=lambda obj: (int(re.sub(r"\D", "", obj.property)), re.sub(r"[^A-Z]+", "", obj.property)) )
for lease in lease_list:
print(lease.text)
只是提高效率的想法:
for line in lines:
l, m = line.split(' - ')
formatted_data.append({'property' : int(re.sub(r"\D", "", l))
, 'literal' : re.sub(r"[^A-Z]", "", l)
, 'm': m
, 'line': line})
formatted_data.sort(key=lambda obj: (obj['property'], obj['literal']))
for lease in formatted_data:
print(lease['line'])
for line in formatted_data:
lease_list.append(Lease(line['m'], line['property'], line['line']))