Stack Exchange
Grouping and sorting files in python
I have tab-delimited file like this,
gene_name               length
Traes_3AS_4F141FD24.2   24.8    
Traes_4AL_A00EF17B2.1   0.0 
Traes_4AL_A00EF17B2.1   0.9 
Traes_4BS_6943FED4B.1   4.5 
Traes_4BS_6943FED4B.1   42.9    
UCW_Tt-k25_contig_29046 0.4 
UCW_Tt-k25_contig_29046 2.8
UCW_Tt-k25_contig_29046 11.4    
UCW_Tt-k25_contig_29046 12.3    
UCW_Tt-k25_contig_29046 14.4
UCW_Tt-k25_contig_29046 14.2    
UCW_Tt-k25_contig_29046 19.6    
UCW_Tt-k25_contig_29046 19.6
UCW_Tt-k25_contig_29046 21.1    
UCW_Tt-k25_contig_29046 23.7    
UCW_Tt-k25_contig_29046 23.7

I need to group by gene_name, and divide file in 3 files: 1) if gene_name is unique 2) if the differences in lengths between the genes inside the group is >10 3) if the differences in lengths inside the group is <10.
This is my try,
from itertools import groupby

def iter_hits(hits):
    for i in range(1,len(hits)):
        (p, c) = hits[i-1], hits[i]
        yield p, c

def is_overlap(hits):
    for p, c in iter_hits(hits):
        if c[1] - p[1] > 10:
            return True

fh = open('my_file','r')
oh1 = open('a', 'w')
oh2 = open('b', 'w')
oh3 = open('c', 'w')

for qid, grp in groupby(fh, lambda l: l.split()[0]):
    hits = []
    for line in grp:
        hsp = line.split()
        hsp[1]= float(hsp[1])
        hits.append(hsp)
    hits.sort(key=lambda x: x[1])
    if len(hits)==1:
        oh = oh3
    elif is_overlap(hits):
        oh = oh1
    else:
        oh = oh2

    for hit in hits:
        oh.write('\t'.join([str(f) for f in hit])+'\n')

The outputs I need are:
c)Traes_3AS_4F141FD24.2   24.8          b)Traes_4AL_A00EF17B2.1   0.0 
                                          Traes_4AL_A00EF17B2.1   0.9 
a)Traes_4BS_6943FED4B.1   4.5 
Traes_4BS_6943FED4B.1   42.9    
UCW_Tt-k25_contig_29046 0.4 
UCW_Tt-k25_contig_29046 2.8
UCW_Tt-k25_contig_29046 11.4    
UCW_Tt-k25_contig_29046 12.3    
UCW_Tt-k25_contig_29046 14.4
UCW_Tt-k25_contig_29046 14.2    
UCW_Tt-k25_contig_29046 19.6    
UCW_Tt-k25_contig_29046 19.6
UCW_Tt-k25_contig_29046 21.1    
UCW_Tt-k25_contig_29046 23.7    
UCW_Tt-k25_contig_29046 23.7

P.S. I'm so sorry for such a long question, but otherwise I risk not to explain well.

What are you getting right now? Are you getting any error?

the gene UCW_Tt-k25_contig_29046 results in file b, I guess this is bcos I am doing a substraction from rthe previou gene length, how to improve this?

You need them to end in c file if there are two values that have difference greater than 10?

no, I need all genes the lengths of which have differences more than 10 to be in a file, i.e 23.7-0.4 > 10 so it should be in a file. Basically, I need to do substractions between all lengths inside each group.

It isn't obvious that the criteria you give correspond to a unique partition of the file.

If your aim is -

I need all genes the lengths of which have differences more than 10 to be in a file, i.e 23.7-0.4 > 10 so it should be in a file.

Then in is_overlap(hits) you can just check the different between the last element and the first element, since you are already sorting them by the second element before calling this function, the last element would be the largest , and the first element would be the smallest.
Hence, you can do -
def is_overlap(hits):
    if hits[-1][1] - hits[0][1] > 10:
        return True


Your data  already seems to be in sorted order which so you just compare the first and last floats from each group:
from itertools import groupby

with  open('a', 'w') as uniq, open('b', 'w') as lt, open('c', 'w') as gt:
    with open("foo.txt") as f:
        next(f)
        for _, v in groupby(f, lambda x: x.split(None, 1)[0]):
            v = list(v)
            if len(v) == 1:
                uniq.write(v[0])
            elif float(v[-1].split(None, 1)[1]) - float(v[0].split(None, 1)[1]) < 10:
                lt.writelines(v)
            elif float(v[-1].split(None, 1)[1]) - float(v[0].split(None, 1)[1]) > 10:
                gt.writelines(v)

