I wrote this code that should parse lines from input file 
Input format : 
movie ID can be of multiple entries so we should calculate the average 
Output: 
\*\*no duplication (which is the problem)

```
import re
f = open("ratings2.txt", "rb")
fo = open("ratings3.txt", "wb")
lines = f.readlines()
movielist=[]
for line in lines:
    m_obj = re.search(r"<(\S+), (\S+)>", line)
    x= m_obj.group(1)
    ratinglist=[]
    if x not in movielist:
        movielist.append(x)
        for subline in lines:
            n_obj = re.search(r"<(\S+), (\S+)>", subline)
            if n_obj.group(1)==x:
                ratinglist.append(float(n_obj.group(2)))
                av= (float(sum(ratinglist))/float(len(ratinglist)))
                final= "<%s, %f>\n" %(n_obj.group(1), av)                
                fo.write(final)
f.close()
fo.close()

```

input file:

```
<122, 5>
<185, 5>
<122,4.5>

```

desired output:

```
<122, 4.75>
<185, 5>

```

but the problem here seems that the code double-loops each instance and add a line of the instance first entry...can anybody help ?

actual output:

```
<122, 5>
<122, 4.75>
<185, 5>

```