Re: list of unique non-subset sets

2005-03-18 Thread les_ander
OK, so I need to be more precise.
Given a list of sets, output the largest list of sets (from this list,
order does not matter) such that:
1) there is no set that is a PROPER subset of another set in this list
2) no two sets have exactly the same members (100% overlap)

Seems like this problem is much harder than I thought...

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


Re: list of unique non-subset sets

2005-03-18 Thread les_ander
Once again my specs were incomplete.
By largest I mean exactly what you pointed out as in sum(map(len,
setlist)).

I think this might work--sorting of the initial list should do the
trick.
1) sort the sets by size (in decending order)
2) put the first (largest) into a new list (Lu)
for s in Lnew[1:]:
 keep=True
 for i in range(len( Lun) ):
   if len(s)==len( s  Lun[i] ):
 keep=False
 break
 if keep==True:
   Lun.append( s )

- here is the complete code
s1=set(['a','b','c'])
s2=set(['a','c'])
s3=set(['a','d','e','f'])
s4=set(['r','k','l'])
s5=set(['r','k','l'])
s6=set(['g', 'h'])
s7=set(['h', 'i'])
s8=set(['g', 'h', 'i'])

L=[s1,s2,s3,s4,s5,s6,s7,s8]
length=[len(s) for s in L]
L2= sorted(zip(length,range(len(L
Lnew=[L[j] for (i,j) in L2]
Lnew.reverse()
Lun=[Lnew[0]]  # list with the result
for s in Lnew[1:]:
 keep=True
 for i in range(len( Lun) ):
   if len(s)==len( s  Lun[i] ):
 keep=False
 break
 if keep==True:
   Lun.append( s )

#
 Lun
[set(['a', 'e', 'd', 'f']), set(['i', 'h', 'g']), set(['k', 'r', 'l']),
set(['a', 'c', 'b'])]

Seems like I got it.

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


list of unique non-subset sets

2005-03-17 Thread les_ander
Hi,
I have many set objects some of which can contain same group of object
while others can be subset of the other. Given a list of sets,
I need to get a list of unique sets such that non of the set is an
subset of another or contain exactly the same members.

Tried to do the following:
s1=set(['a','b','c'])
s2=set(['a','c'])
s3=set(['a','d','e','f'])
s4=set(['r','k','l'])
s5=set(['r','k','l'])
L=[s1,s2,s3,s4,s5]
---  cleaned-up list should contain s1, s3, s5

Lu=[] # unique list of sets
Lu.append( L[0] )
for i in range(1,len(L)):
 s=L[i]
 # check for equality
 if s in L:
continue
 # check for subseting
 for j in len(Lu):
s1=Lu[j]
if len (s-s1)==0:
  continue
elif len(s1-s)==0:
Lu[i]=s1 # replace with the bigger


But this does not work since I get
Lu=[set(['a', 'c', 'b'])]

What am I doing wrong?
thanks

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


generic text read function

2005-03-17 Thread les_ander
Hi,
matlab has a useful function called textread which I am trying to
reproduce
in python.

two inputs: filename, format (%s for string, %d for integers, etc and
arbitary delimiters)

variable number of outputs (to correspond to the format given as
input);

So suppose your file looked like this
str1 5 2.12
str1 3 0.11
etc with tab delimited columns.
then you would call it as

c1,c2,c3=textread(filename, '%s\t%d\t%f')

Unfortunately I do not know how to read a line from a file
using the line format given as above. Any help would be much
appreciated
les

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


a wrapper to invoke functions using argument

2005-02-22 Thread les_ander
Hi,
support I have a library of function, called mylib.py, in which
there are 2 functions 'f1' and 'f2' (1 arguments in either one);

Now I want to write a wrapper that will invoke f1 or f2 using the
command line argument. So for example, I want to write a function
call.py and invoke it as

python call.py f1 arg1

So this is straight forward, but I don't know how to evaluate a
function.
any help would be much appreciate it.
les

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


intersection of 2 list of pairs

2005-02-20 Thread les_ander
Hi,
I have 2 lists of tuples that look like:
E1=[('a','g'),('r','s')] and
E2=[('g','a'),('r','q'),('f','h')].
In this tuple, the ordering does not
matter, i.e. (u,v) is the same as (v,u).

What I want to do is the following:
given 2 list of tuples, E1 and E2, I want to create another list with
tuples that are common to both. So in the above example I would like
to return ('a','g') as being common.
So far I have the code below but it does not work out properly, I would
be grateful if someone can help me out .
thanks

def list_of_tuples_intersect(E1,E2):

S1=Set()
S2=Set()
for e in E1:
S1.add((e[0],e[1]))
S1.add((e[1],e[0]))
for e in E2:
S2.add((e[0],e[1]))
S2.add((e[1],e[0]))
S= S1  S2
SS=Set()
done=Set()

for e in S:
if ((e[0],e[1])  in done) or ((e[1],e[0])  in done):
continue
else:
SS.add((e[0],e[1]))
done.add((e[0],e[1]))
done.add((e[1],e[0]))
return SS

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


regular expression: perl == python

2004-12-21 Thread les_ander
Hi,
i am so use to perl's regular expression that i find it hard
to memorize the functions in python; so i would appreciate if
people can tell me some equivalents.

1) In perl:
$line = The food is under the bar in the barn.;
if ( $line =~ /foo(.*)bar/ ) { print got $1\n; }

in python, I don't know how I can do this?
How does one capture the $1? (I know it is \1 but it is still not clear
how I can simply print it.
thanks

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


How do I define the search path for personal library

2004-12-18 Thread les_ander
Dear all,
i have a simple question.
Suppose I have my classes such as
myClass1.py
myClass2.py
etc
which I keep in a special folder ~/py_libs

Now
suppose I have a program that is not in py_libs
but I want to do
import myClass1 # note: myClass1 is not in the current directory

how can I set the search path for python so that it can
find myClass1?
thanks

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