split function

M

Mohammed Altaj

Dear all

Sorry , I confused between two things , what i said in the last e-mail i
already managed to do using C code , But what i need to do using python
is : my input data :

0 2 3 4
1 2 4
2 3
3 4

what i suppose to do is , using the first line and start searching
number by number ,first i have 0 search in the rest of lines if there is
0 print out the all numbers except 0 , after that , start searching
using the 2ed element in the first line which is 2 , in the 2ed line we
have 1 , 4 . in the 3rd line we have 3 , in the 4th line we do not have
2. And so on for 3 and 4 , and also for the 2nd , 3rd lines , so the
output should be

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

And i managed to do this , but i did in the case of no space between
numbers,when i am reading from file , like
0234
124
23
34

I want my code be able to deal with the space between numbers , and this
is my code again
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()


Thanks
 
B

bruno modulix

Mohammed said:
Dear all

Sorry , I confused between two things , what i said in the last e-mail i
already managed to do using C code , But what i need to do using python
is : my input data :

0 2 3 4
1 2 4
2 3
3 4

what i suppose to do is , using the first line and start searching
number by number ,first i have 0 search in the rest of lines if there is
0 print out the all numbers except 0 , after that , start searching
using the 2ed element in the first line which is 2 , in the 2ed line we
have 1 , 4 . in the 3rd line we have 3 , in the 4th line we do not have
2. And so on for 3 and 4 , and also for the 2nd , 3rd lines , so the
output should be

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

And i managed to do this , but i did in the case of no space between
numbers,when i am reading from file , like
0234
124
23
34

I want my code be able to deal with the space between numbers ,

"0 2 1 3\n".strip().replace(' ', '')
=> "0213"
and this
is my code again

(snip unreadable and overcomplicated implementation)

in_file = open('result.dat','r')
lines = [line.strip().replace(' ', '') for line in in_file]
in_file.close()

def find_relateds(target, lines):
relateds = [target]
for line in lines:
if target in line:
relateds.extend(list(line.replace(target, '')))
return relateds

out_file=open('result2.dat','w')
for numline, line in enumerate(lines):
buf = []
for target in line:
relateds = find_relateds(target, lines[numline+1:])
buf.append(" ".join(relateds))
buf = " ".join(buf)
print buf
out_file.write("%s\n" % buf)

out_file.close()

BTW, note that this won't work with numbers > 9. If you need to deal
with such a case, you can use str.split() to turn "10 21 32 43" into
["10", "21", "32", "43"]. Adapting the above code snippet to use this
scheme should be pretty straightforward.
 
B

Bengt Richter

Dear all

Sorry , I confused between two things , what i said in the last e-mail i
already managed to do using C code , But what i need to do using python
is : my input data :

0 2 3 4
1 2 4
2 3
3 4

what i suppose to do is , using the first line and start searching
number by number ,first i have 0 search in the rest of lines if there is
0 print out the all numbers except 0 , after that , start searching
using the 2ed element in the first line which is 2 , in the 2ed line we
have 1 , 4 . in the 3rd line we have 3 , in the 4th line we do not have
2. And so on for 3 and 4 , and also for the 2nd , 3rd lines , so the
output should be

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

And i managed to do this , but i did in the case of no space between
numbers,when i am reading from file , like
0234
124
23
34

I want my code be able to deal with the space between numbers , and this
is my code again
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()


Thanks


Does this do what you want? (except that it writes to stdout and doesn't print)?
(note that I get 234 instead of 23 on the 3rd line of output)
... 0 2 3 4
... 1 2 4
... 2 3
... 3 4
... """)
>>> import sys
>>> out_file = sys.stdout
>>>
>>> lines = [''.join(line.split()) for line in in_file] # clean out spaces and '\n'
>>> for i, line in enumerate(lines):
... for digit in line:
... out_file.write(digit)
... for followingline in lines[i+1:]:
... if digit not in followingline: continue
... line_sans_digit = followingline.replace(digit,'')
... out_file.write(line_sans_digit)
... out_file.write("\n")
...
021433244123
12343
234
34

Also note that I eliminated all duplicate digits from a selected line, rather than just the
first match. Easy to change.

Regards,
Bengt Richter
 

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,982
Messages
2,570,190
Members
46,740
Latest member
AdolphBig6

Latest Threads

Top