intersection of 2 list of pairs
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
a wrapper to invoke functions using argument
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
list of unique non-subset sets
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
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
Re: list of unique non-subset sets
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
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
how do I "peek" into the next line?
Hi, suppose I am reading lines from a file or stdin. I want to just "peek" in to the next line, and if it starts with a special character I want to break out of a for loop, other wise I want to do readline(). Is there a way to do this? for example: while 1: line=stdin.peek_nextline() if not line: break if line[0]=="|": break: else: x=stdin.nextline() # do something with x thanks -- http://mail.python.org/mailman/listinfo/python-list
Re: how do I "peek" into the next line?
I should have also said that I am using the same file handle. So suppose the file handle is fp then I read some lines from fp and I hand it over to other functions that read other things from fp (in an ordered manner). les -- http://mail.python.org/mailman/listinfo/python-list
Re: how do I "peek" into the next line?
OK, I am sorry , I did not explain my problem completely. I can easily break from the loop when I see the character in a line that I just read; however my problem involves putting back the line I just read since if I have seen this special character, I have read one line too many. Let me illustrate suppose the file has 3 lines line1.line1.line1 >line2.line2.line line3.line3.line3 now suppose I have read the first line already. then I read the second line and notice that there is a ">" in front (my special character) then I want the put back the second line into the file or the stdin. An easy way is if i read all the lines in to an array and then I can put back the line with the special character back into this array. However, this file I am readding is huge! and I can read it all in one swoop (not enough memory). for x in stdin: if x[0]==">": put back the x some how... <- break else: print x I hope this is clear thanks -- http://mail.python.org/mailman/listinfo/python-list
How do I define the search path for personal library
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
Re: How do I define the search path for personal library
Yes, this is exactly what I wanted--just like in perl I can add search path to @inc. thanks -- http://mail.python.org/mailman/listinfo/python-list
input record seperator (equivalent of "$|" of perl)
Hi, I know that i can do readline() from a file object. However, how can I read till a specific seperator? for exmple, if my files are name profession id # name2 profession3 id2 I would like to read this file as a record. I can do this in perl by defining a record seperator; is there an equivalent in python? thanks -- http://mail.python.org/mailman/listinfo/python-list
regular expression: perl ==> python
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