3

I have this file:

-0
1
16
9
-00
1
3
4
0
7
9
-000
...

and I want to sort them and store them to a file.

I read the file store them inside a list , sort the list and then save the list to a file. Problem is that It starts from the second -x .

 for line in file:
        temp_buffer = line.split()
        for i,word in enumerate(temp_buffer):
            if "-" not in word:
                if word in index_dict:
                    l1.append(word)
                else:
                    l1.append(function(word))
            else:
                l1.append(word)
                l1.sort()
                print(l1 , file=testfile)
                del l1
                l1 = []

So the 1st loop it goes to the else statment and stores just the first -0 without the words beween -0 and -00. How should I fix this ? I want the output to be like that :

-0
1
9
16
-00
0
1
3
4
7
9
-000
....

2 Answers 2

7

You can use itertools.groupby to "partition" the data into groups between lines starting with - instead. Where it starts with - write the line out, otherwise, write the sorted lines, eg:

from itertools import groupby

with open('input') as fin, open('output', 'w') as fout:
  for k, g in groupby(fin, lambda L: L.startswith('-')):
    if k:
      fout.writelines(g)
    else:
      fout.writelines(sorted(g, key=int))
Sign up to request clarification or add additional context in comments.

4 Comments

will this sort the list in every loop ? because i need to keep the complexity low.
It will sort the items between each line starting with -
input file is huge so i need to group the elements and when the group is full(all the elements between -x -xxxx) then store it to the file. will this function try to read all the file and try to group it and then store it ?
@Bill no... it will consume and sort the parts between the -'s as it goes... It will only have in memory the lines between a single set of markers at any one time
0

You may use this :

l1=[]
for i,word in enumerate(temp_buffer):
            if "-" in word:
                l1.sort()
                print(l1)
                l1 = []
            if word in index_dict:
                l1.append(word)
            else:
                l1.append(function(word))

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.