split function

M

Mohammed Altaj

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

William Park

Mohammed Altaj said:
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

Use Python's dictionary (also known as associative array or hash). Read
documentation.

--
William Park <[email protected]>, Toronto, Canada
ThinFlash: Linux thin-client on USB key (flash) drive
http://home.eol.ca/~parkw/thinflash.html
BashDiff: Super Bash shell
http://freshmeat.net/projects/bashdiff/
 
R

rafi

Mohammed said:
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

with a two pass processing (one for building the output, one for its
printing), using a dict to make lists for each index (everything is
treated as strings as you do not compare nor compute them)

results = dict ()

input = file ('input.dat')

for line in input:
idx, value = line.split ()
if idx in results:
results [idx] .append (value)
else:
results [idx] = [value]

input.close ()

output = open ('output.dat', 'w')

for idx, values in results.items ():
output.write ('%s %s\n' % (idx, ' '.join (values)))

output.close ()

you can define two function, in case of your output may vary in the future.
> [snip your code]

8 levels of indentation seems really to much for a good ppiece of code
from my point of view.

my 2 cents
 
B

bruno modulix

Mohammed said:
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

Remove them ?
This is my code

Your code does not seems to work as expected. I get:
0 3 4 2 1 3
0 4 3 2
0 4 1
1 4 2 3
1 4
2 3
3
def belong_to(x,a):
c=-1
for i in range(len(a)-1):
if x==int(a):
c=i
return c


def belong_to(x, line):
for i, c in enumerate(line.strip()):
if x == int(c):
return i
return -1

def belong_to(x, line):
return line.find(str(x))

belong_to = lambda x, line: line.find(str(x))

Now, is it useful to define a function for such a trivial test ?

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


def list_belong(line, lines):
return line in lines

.... is it really useful to define a function for such a trivial test ?
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)


ouch :(
print
out_file.write("\n")

out_file.close()
in_file.close()

May I suggest a much more simple version that conform to your specs and
solves the space problem ?

from itertools import groupby

in_file=open('data.dat','r')

# strip EOLs and get rid of whitespaces
lines = [line.strip().replace(' ', '') for line in_file.readlines()]
in_file.close()

out_file=open('result.dat','w')

# group lines by first char
for key, groups in groupby(lines, lambda line: line[0]):
buf = "%s %s" % (key, " ".join([group[1] for group in groups]))
print buf
out_file.write("%s\n" % buf)

out_file.close()


Python is meant to make your life easier...
 

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

No members online now.

Forum statistics

Threads
473,780
Messages
2,569,611
Members
45,282
Latest member
RoseannaBa

Latest Threads

Top