split function
Mohammed Altaj
mohammed at aims.ac.za
Mon Aug 22 15:44:49 EDT 2005
Dear All
What i want to do is , my input is like
0 2
0 3
0 4
1 2
1 4
2 3
3 4
I am comparing and put the number in group , like ,the first three lines
, all has zero as first input for each line, so the out put should look
like
0 2 3 4
and so on
1 2 4
2 3
3 4
I managed to do what i need , but i did in this case , there is no space
between numbers , like
02
03
04
12
14
23
34
so , how can i do this with spaces between numbers
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()
More information about the Python-list
mailing list