Lists of list

2005-08-17 Thread Mohammed Altaj
Hi All

I am having problem with delete line if its belong to another one , example

['0132442\n', '13\n', '24\n']

the 2nd and 3rd are already in the first line , how can do this !!!

Thanks
-- 
http://mail.python.org/mailman/listinfo/python-list


List of strings

2005-08-17 Thread Mohammed Altaj
Hi All

Thanks for your reply , what i am doing is , i am reading from file ,
using readlines() , I would like to check in these lines , if there is
line belong to another one or not , if it is , then i would like to
delete it

['0132442\n', '13\n', '24\n'] 

'13' is already in '0132442'
'24' is already in '0132442' 

Thanks 



-- 
http://mail.python.org/mailman/listinfo/python-list


Re: List of string

2005-08-18 Thread Mohammed Altaj

>
>
>  
>
>
>Mohammed Altaj wrote:
>  
>
>>Hi All
>>
>>I am having problem with delete line if its belong to another one , example
>>
>>
>
>I think, you mean to remove all lines that are substrings of another
>line.
>
>l = ['0132442\n', '13\n', '24\n']
>l = [e.strip() for e in l]
>
>i = 0
>while True:
>  try:
>for j in range(len(l)):
>  if i == j:
>continue
>  if l[j].find(l[i]) >= 0:
># line 'j' is superstring of line 'i'
>del l[i]
>break
>else: # doesn't have superstring
>  i += 1
>  except IndexError:
>break
>
>Basically, I try all n*n combinations, and remove substring lines
>"in-place".
>
>BranoZ
>
>
>  
>
>
Thanks , but , this work for an ordered substrings , just like what we
had   ['0132442\n', '13\n', '24\n'] , I would like to remove all
substrings from the list , example

['0134314244133', '132443', '234'] 


2nd and 3rd strings are also substrings from the 1st one , so it should
be removed

Thanks



-- 
http://mail.python.org/mailman/listinfo/python-list


split function

2005-08-22 Thread Mohammed Altaj

 Hi All

 I am reading data from file using readlines() to list , my question
is how to deal with space between any two numbers , I mean , my data
looks like
1 3
3 4
5 2
6 1

I tried to with my data as it, but i couldn't , so i removed the spaces
13
34
52
61

But when i deal with large number > 9  , i tried to use split
function , but doesn't work , I can attach my code if its good idea to do ,,

Thanks











-- 
http://mail.python.org/mailman/listinfo/python-list


split function

2005-08-22 Thread 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[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()






-- 
http://mail.python.org/mailman/listinfo/python-list


split function

2005-08-22 Thread 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[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()


Thanks


-- 
http://mail.python.org/mailman/listinfo/python-list


how to deal with space between numbers

2005-08-23 Thread 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[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()



Thank you all

-- 
http://mail.python.org/mailman/listinfo/python-list


Re:how to deal with space between numbers

2005-08-23 Thread Mohammed Altaj
>
>
>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)
>
>
>
>  
>
>  
>
 

 Thanks a lot for your valuable answer, i like the way you code , but i
would like to use my own, so if it is possible for you and if you have
time, please could you fix my code, so that i can do what i want. 
Because i am using the this out put to another one , and i have the same
problem.  I will really appreciate it !!!


  Regards

 


-- 
http://mail.python.org/mailman/listinfo/python-list