how to deal with space between numbers

Bengt Richter bokr at oz.net
Tue Aug 23 22:45:37 EDT 2005


On Tue, 23 Aug 2005 19:20:15 +0200, Mohammed Altaj <mohammed at aims.ac.za> wrote:

>
>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]):
This was selecting single characters at a[i], so it
was a misleading clue re your actual requirements ;-)

>            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
>
You could try dealing with the data as lists of numbers, e.g, (slight mod from my previous)

 >>> # test with string file input and stdout output
 ... import StringIO
 >>> in_file = StringIO.StringIO("""\
 ... 0 2 3 4
 ... 1 2 4
 ... 2 3
 ... 3 4
 ... 0 1 5 9
 ... 1 12 10
 ... 4 6 7
 ... 10 9
 ... """)
 >>> import sys
 >>> out_file = sys.stdout
 >>>
 >>> lines = [map(int,line.split()) for line in in_file] # make lines as int lists
 >>> for i, line in enumerate(lines):
 ...     out = []
 ...     for digit in line:
 ...         out.append(digit)
 ...         for followingline in lines[i+1:]:
 ...             if digit in followingline:
 ...                 out.extend([x for x in followingline if x != digit])
 ...     out_file.write(' '.join(map(str, out))+"\n")
 ...
 0 1 5 9 2 1 4 3 3 2 4 4 1 2 3 6 7
 1 0 5 9 12 10 2 3 4 3 6 7
 2 3 4
 3 4 6 7
 0 1 12 10 5 9 10
 1 12 10 9
 4 6 7
 10 9

(The output now has to have spaces as well, to delimit the numbers).

Regards,
Bengt Richter



More information about the Python-list mailing list