Python没有按预期排序

时间:2016-01-21 17:02:00

标签: python sorting alphanumeric

我正在尝试使用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

1 个答案:

答案 0 :(得分:3)

您正在删除此处的文字

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