1

I have written the following script. It opens a file, reads each line from it splitting by new line character and deleting first character in line. If line exists it's being added to array. Next each element of array is splitted by whitespace, sorted alphabetically and joined again. Every line is printed because script is fired from console and writes everything to file using standard output. I'd like to optimize this code to be more pythonic. Any ideas ?

import sys def main(): filename = sys.argv[1] file = open(filename) arr = [] for line in file: line = line[1:].replace("\n", "") if line: arr.append(line) for line in arr: lines = line.split(" ") lines.sort(key=str.lower) line = ''.join(lines) print line if __name__ == '__main__': main() 
1
  • 2
    Why create the list arr? The file is already a sequence. Why are you creating arr and not doing anything with it? Commented Jan 8, 2011 at 12:48

5 Answers 5

2
def main(): file = open(sys.argv[1]) for line in file: if line.rstrip(): print ''.join(sorted(line[1:-1].split(), key=str.lower())) 
Sign up to request clarification or add additional context in comments.

2 Comments

Shouldn't be if line.strip()?
@rubik: yes thanks for the remark ; but rstrip() is much preferable in this case.
1

Why create the list arr? The file is already a sequence. Why are you creating arr and not doing anything with it except iterating again.

for line in file: line = line[1:].replace("\n", "") if not line: continue lines = line.split(" ") lines.sort(key=str.lower) line = ''.join(lines) print line 

2 Comments

almost ok but this adds the empty lines
This is a good version, high readability. Which I think should count for the most.
0

You can condense the second loop in the first one:

import sys def main(): filename = sys.argv[1] file = open(filename) for line in file: if line.strip(): lines = line.split(" ") lines.sort(key=str.lower) print ''.join(lines) if __name__ == '__main__': main() 

Comments

0

for small files:

import fileinput lines = [] for line in fileinput.input(): line = line[1:].strip() if line: words = line.split() words.sort(key=str.lower) lines.append(' '.join(words)) print '\n'.join(lines) 

for big files:

import fileinput for line in fileinput.input(): line = line[1:].strip() if line: words = line.split() words.sort(key=str.lower) print ' '.join(words) 

Comments

0
import fileinput def main(): for line in fileinput.input(): words = line[1:].split() # strip() is redundant if words: words.sort(key=str.lower) print ' '.join(words) if __name__=="__main__": main() 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.