用字符串中的两位数排序 - Python

时间:2015-06-13 00:10:14

标签: python python-2.7

我是Python的新手,我很难解决这个问题。 我正在尝试对列表进行排序,以便能够对其进行排序1)通过第一个数字和2)第二个数字。我想有这样的事情:

'1-1bird'
'1-1mouse'
'1-1nmouses'
'1-2mouse'
'1-2nmouses'
'1-3bird'
'10-1birds'
(...)

这些数字可以是1到99 ex:99-99bird是可能的。 这是我经历过几次令人头疼的问题之后的代码。能够按照以下第一个字母排序将是一个奖励 这是我尝试过的:

#!/usr/bin/python


myList = list()
myList = ['1-10bird', '1-10mouse', '1-10nmouses', '1-10person', '1-10cat', '1-11bird', '1-11mouse', '1-11nmouses', '1-11person', '1-11cat', '1-12bird', '1-12mouse', '1-12nmouses', '1-12person', '1-13mouse', '1-13nmouses', '1-13person', '1-14bird', '1-14mouse', '1-14nmouses', '1-14person', '1-14cat', '1-15cat', '1-1bird', '1-1mouse', '1-1nmouses', '1-1person', '1-1cat', '1-2bird', '1-2mouse', '1-2nmouses', '1-2person', '1-2cat', '1-3bird', '1-3mouse', '1-3nmouses', '1-3person', '1-3cat', '2-14cat', '2-15cat', '2-16cat', '2-1bird', '2-1mouse', '2-1nmouses', '2-1person', '2-1cat', '2-2bird', '2-2mouse', '2-2nmouses', '2-2person']


def mysort(x,y):
x1=""
y1=""

for myletter in x :
    if myletter.isdigit() or "-" in myletter: 
        x1=x1+myletter

x1 = x1.split("-")

for myletter in y :
    if myletter.isdigit() or "-" in myletter: 
        y1=y1+myletter

y1 = y1.split("-")


if x1[0]>y1[0]:
    return 1
elif x1[0]==y1[0]:
    if x1[1]>y1[1]:
        return 1
    elif x1==y1:
        return 0
    else :
        return -1
else :
    return -1




myList.sort(mysort)
print myList

谢谢!

马丁

3 个答案:

答案 0 :(得分:0)

您在'-'上使用isalpha()isdigit()分享了一些好主意,但我们会使用这些创建一个接收项目并返回的函数a"清洁"该项目的版本,可以很容易地排序。它将创建第一个数字的三位数,零填充表示,然后是第二个数字的类似事物,然后是"字"部分(而不仅仅是第一个字符)。结果看起来像"001001bird"(不会显示 - 它只是在内部使用)。内置函数sorted()将使用此回调函数作为键,获取每个元素,将其传递给回调,并将返回值基于排序顺序。在测试中,我使用*运算符和sep参数来打印它而不需要构造循环,但循环也很好。

def callback(item):
    phrase = item.split('-')
    first = phrase[0].rjust(3, '0')
    second = ''.join(filter(str.isdigit, phrase[1])).rjust(3, '0')
    word = ''.join(filter(str.isalpha, phrase[1]))
    return first + second + word

测试:

>>> myList = ['1-10bird', '1-10mouse', '1-10nmouses', '1-10person', '1-10cat', '1-11bird', '1-11mouse', '1-11nmouses', '1-11person', '1-11cat', '1-12bird', '1-12mouse', '1-12nmouses', '1-12person', '1-13mouse', '1-13nmouses', '1-13person', '1-14bird', '1-14mouse', '1-14nmouses', '1-14person', '1-14cat', '1-15cat', '1-1bird', '1-1mouse', '1-1nmouses', '1-1person', '1-1cat', '1-2bird', '1-2mouse', '1-2nmouses', '1-2person', '1-2cat', '1-3bird', '1-3mouse', '1-3nmouses', '1-3person', '1-3cat', '2-14cat', '2-15cat', '2-16cat', '2-1bird', '2-1mouse', '2-1nmouses', '2-1person', '2-1cat', '2-2bird', '2-2mouse', '2-2nmouses', '2-2person']
>>> print(*sorted(myList, key=callback), sep='\n')
1-1bird
1-1cat
1-1mouse
1-1nmouses
1-1person
1-2bird
1-2cat
1-2mouse
1-2nmouses
1-2person
1-3bird
1-3cat
1-3mouse
1-3nmouses
1-3person
1-10bird
1-10cat
1-10mouse
1-10nmouses
1-10person
1-11bird
1-11cat
1-11mouse
1-11nmouses
1-11person
1-12bird
1-12mouse
1-12nmouses
1-12person
1-13mouse
1-13nmouses
1-13person
1-14bird
1-14cat
1-14mouse
1-14nmouses
1-14person
1-15cat
2-1bird
2-1cat
2-1mouse
2-1nmouses
2-1person
2-2bird
2-2mouse
2-2nmouses
2-2person
2-14cat
2-15cat
2-16cat

答案 1 :(得分:0)

你需要前导零。字符串按字母顺序排序,顺序与数字顺序不同。它应该是

'01-1bird'
'01-1mouse'
'01-1nmouses'
'01-2mouse'
'01-2nmouses'
'01-3bird'
'10-1birds'

正如你所看到的,1在0之后。

答案 2 :(得分:0)

这里的其他答案非常值得尊重,我敢肯定,但是为了获得完全的功劳,你应该确保你的答案适合单行,并使用尽可能多的列表理解:

import itertools
[''.join(r) for r in sorted([[''.join(x) for _, x in
           itertools.groupby(v, key=str.isdigit)]
           for v in myList], key=lambda v: (int(v[0]), int(v[2]), v[3]))]

这应该做得很好:

['1-1bird',
 '1-1cat',
 '1-1mouse',
 '1-1nmouses',
 '1-1person',
 '1-2bird',
 '1-2cat',
 '1-2mouse',
 ...
 '2-2person',
 '2-14cat',
 '2-15cat',
 '2-16cat']