how to deal with space between numbers

M

Mohammed Altaj

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):
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==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)
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,
out_file.write(line)



print
out_file.write("\n")

out_file.close()
in_file.close()



Thank you all
 
B

bruno modulix

Mohammed said:
Dear All

This is my problem again , (snip)

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

Read my answers to your two previous posts.
(in short : use str.split() - but *do* read my answers if you want to
save you a lot of pain)
 
R

rafi

bruno said:
Read my answers to your two previous posts.
(in short : use str.split() - but *do* read my answers if you want to
save you a lot of pain)

especially when several persons replied to you...
 
B

Bengt Richter

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):

This was selecting single characters at a, 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==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)
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,
out_file.write(line)



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)
... 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
 
S

Sybren Stuvel

Mohammed Altaj enlightened us with:
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)

Keep the spaces, and use thestring.split()

Sybren
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,767
Messages
2,569,572
Members
45,046
Latest member
Gavizuho

Latest Threads

Top