python文件内容排序

时间:2014-04-25 03:30:26

标签: python sorting

我的文件内容如下:

Dnext_0[11]
Dnext_1[0]
Dnext_0[0]
Dnext_0[128]
Dnext_0[1]

排序后,我希望:

Dnext_0[0]
Dnext_0[1]
Dnext_0[11]
Dnext_0[128]
Dnext_1[0]

我试过这个:

with open('testfile') as f:
    sorted_file = sorted(f)

print sorted_file

但它没有给我我想要的东西。

5 个答案:

答案 0 :(得分:1)

我们的想法是使用sorted()对指定key lambda函数的数据进行排序,该函数使用{{3提取Dnext_之后和括号内的数字}}:

import re

pattern = re.compile('Dnext_(\d+)\[(\d+)\]')
with open('input.txt') as f:
    print sorted(f, key=lambda x: map(int, pattern.search(x).groups()))

打印:

Dnext_0[0]
Dnext_0[1]
Dnext_0[11]
Dnext_0[128]
Dnext_1[0]

Dnext_(\d+)\[(\d+)\]正则表达式使用捕获组来提取Dnext_之后的数字和括号中的数字:

>>> import re
>>> pattern = re.compile('Dnext_(\d+)\[(\d+)\]')
>>> pattern.search('Dnext_0[11]').groups()
('0', '11')

map(int, ... )有助于将提取数转换为python整数(参见regular expression):

>>> map(int, pattern.search('Dnext_0[11]').groups())
[0, 11]

希望有所帮助。

答案 1 :(得分:0)

创建一个函数,返回一个元组,其中包含您想要排序的元素,并将其用作关键字。

sorted(f.readlines(), key=lambda x:(int(x[6]), int(x.split('[')[1].split(']')[0])))

答案 2 :(得分:0)

看看这是否有帮助..结果应该是: ['Dnext_0 [0]','Dnext_0 [11]','Dnext_0 [128]','Dnext_0 [1]','Dnext_1 [0]']

with open('testfile') as f:
    l=f.readline()
    a=l.split()
    b=sorted(a)

print "original data is is ",l
print "Original data with comma is ",a
print "sorted data is ",b

当我发布答案时,问题发生了变化。下面的回答并不完美,但可以帮到你:

with open('testfile') as f:
    l=f.readlines()
    #=l.split()
    b=sorted(l)

print "original data is is ",l
#print "Original data with comma is ",a
print "sorted data is ",b
for i in b:
    print i

答案 3 :(得分:0)

默认比较方式是将输入视为纯字符串,并且它不知道整数应该被区别对待。

你的方式在Dnext_0 [1]之前得到Dnext_0 [128]是因为' 2'小于']。 要获得预期的结果,您需要告诉已排序的函数如何排序,给它比较什么(key参数),或告诉它如何比较两个字符串(cmp参数) 。请参阅其他答案。

答案 4 :(得分:0)

您可以定义一个函数sorting_criteria(),它定义了如何对文件中的行进行排序:

#!/usr/bin/env python
import re

def sorting_criteria(line):
    """Dnext_0[11] -> [0, 11]"""
    m = re.search(r'_(\d+)\[(\d+)\]\s*$', line, re.M)
    return map(int, m.groups()) if m else []

with open('testfile') as file:
    sorted_file = sorted(map(str.strip, file), key=sorting_criteria)
print '\n'.join(sorted_file)

Output

Dnext_0[0]
Dnext_0[1]
Dnext_0[11]
Dnext_0[128]
Dnext_1[0]