how to deal with space between numbers

Mohammed Altaj mohammed at aims.ac.za
Tue Aug 23 13:20:15 EDT 2005


Dear All

This is my problem again , I tried to sort it out , but i couldn't , I
am reading data from file using readlines , my input like : 

0 1 2 4
1 2 4
2 3
3 4 

What i am doing is , starting with the first element in the first line (
which is 0 in this case )and do search in the other lines , if i found
another 0 , i will save (print out) all the elements except 0 , (in this
is case i have no 0 elsewhere) so i will print only 0.  Now do search by
the 2nd element in the first line (which is 1 in this case) , in the 2nd
line we have 1 , so i should save(print out) all elements except 1 which
are 2 4 , and so on for the rest of the first line , and for the rest of
the file , my out put should be

0 1 2 4 2 1 4 3 4 1 2 3
1 2 3 4 3
2 3 4
3 4

I managed to do all these things , but i did it in the way that i am
reading my data as strings ( no space between numbers) something like

0124
124
23
34

what i would like to know or to do is , how can i deal with my data
after reading it as strings(i need the space between numbers) because i
had problem when dealing with number larger than 9 , example : 

0 1 5 9
1 12 10
4 6 7
10 9

so , when i remove the space between numbers , i loose all my data , i
mean it will look like
0159
11210
467
509


This is my code :


def belong_to(x,a):
    c=-1
    for i in range(len(a)-1):
        if x==int(a[i]):
            c=i
    return c

def list_belong(x,a):            # This function to check if this line
    c=-1                         # line has been searched before or not
    for i in range(len(a)):
        if a[i]==x:
            c=1
            break
    return c

x=0
occur=[]

in_file=open('data.dat','r')
out_file=open('result.dat','w')
fileList = in_file.readlines()
for k in fileList:
    v=k
    occur.append(k)
    n=len(v)-1
    for i in range(n):
        temp=int(v[i])
        print temp,
        out_file.write(str(temp))
        for line in fileList:
            if v!=line:
                if list_belong(line,occur)!=1:
                    if belong_to(temp,line) != -1:
                        j=belong_to(temp,line)
                        for i in range(len(line)-1):
                            if i!=j:
                                print line[i],
                                out_file.write(line[i])
                               
                                
                              
    print
    out_file.write("\n")

out_file.close()
in_file.close()



Thank you all




More information about the Python-list mailing list