1

My file content is below:

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

After sorting, I want to have:

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

I have tried this:

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

print sorted_file

But it does not give me what I want.

5 Answers 5

1

The idea is to use sorted() to sort the data you have specifying key lambda function that extracts the numbers after the Dnext_ and inside the brackets using regular expression:

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()))

prints:

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

Dnext_(\d+)\[(\d+)\] regular expression uses capturing groups to extract the number after Dnext_ and the number in the brackets:

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

map(int, ... ) helps to convert the extract numbers to python integers (see map()):

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

Hope that helps.

Sign up to request clarification or add additional context in comments.

Comments

0

See if this helps .. Result should be : ['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

The question was changed when i posted the answer .Below answer is not perfect but may help you :

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

Comments

0

Make a function returning a tuple containing what you want to order lines by, and use that as the key.

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

Comments

0

default compare way is to treat the input as pure string, and it doesn't know about the integer should be treated differently than others.

Your way gets Dnext_0[128] before Dnext_0[1] is because '2' is smaller than ']'. To get the expected result, you need to tell the sorted function how to sort, by giving it what to compare (the key argument), or telling it how to compare two string (the cmp argument). Refer to other answers.

Comments

0

You could define a function sorting_criteria() that defines how do you want to sort the lines from the file:

#!/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]

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.