Generating multiple lists from one list
hello ppl, Consider a list like ['a.1','b.3','b.4','c.2']. Here 'a','b','c' are objects and 1,3,4,2 are their instance ids and they are unique e.g. a.1 and b.1 cannot exist together. From this list i want to generate multiple lists such that each list must have one and only one instance of every object. Thus, for the above list, my output should be: [['a.1','b.3','c.2'],['a.1','b.4','c.2']] Another example: Let l = ['a.1','b.3','b.4','c.2','c.6','d.3']. Then output should be [['a.1','b.3','c.2','d.3'],['a.1','b.3','c.6','d.3'], ['a.1','b.4','c.2','d.3'],[['a.1','b.4','c.6','d.3'] Can anyone suggest me a time-efficient method for doing this?? TIA, girish -- http://mail.python.org/mailman/listinfo/python-list
Generating multiple lists from one list
hello ppl, Consider a list like ['a.1','b.3','b.4','c.2']. Here 'a','b','c' are objects and 1,3,4,2 are their instance ids and they are unique e.g. a.1 and b.1 cannot exist together. From this list i want to generate multiple lists such that each list must have one and only one instance of every object. Thus, for the above list, my output should be: [['a.1','b.3','c.2'],['a.1','b.4','c.2']] Another example: Let l = ['a.1','b.3','b.4','c.2','c.6','d.3']. Then output should be [['a.1','b.3','c.2','d.3'],['a.1','b.3','c.6','d.3'], ['a.1','b.4','c.2','d.3'],[['a.1','b.4','c.6','d.3'] Can anyone suggest me a time-efficient method for doing this?? TIA, girish -- http://mail.python.org/mailman/listinfo/python-list
How to measure execution time of a program
Hi all, Can anyone tell me the simplest way to do it (some code snippet that could be included in the program's main function) ?? Thanks, girish -- http://mail.python.org/mailman/listinfo/python-list
Re: How to measure execution time of a program
Sorry for spamming again, but please also enlighten me with some way to time a function i.e. to find out how much time each function takes for execution in a big program. Hi all, Can anyone tell me the simplest way to do it (some code snippet that could be included in the program's main function) ?? Thanks, girish -- http://mail.python.org/mailman/listinfo/python-list -- http://mail.python.org/mailman/listinfo/python-list
Function to prune dictionary keys not working
hi ppl, Here is a simple function to remove those keys of a dictionary whose values are less than some specified value. But it isnt working. Please help. def prune(d,cp): l = [] for rule,value in d.iteritems(): #print value if value = cp: l.append(rule) return l d = {'be=c': '1.00', 'c=da': '0.50', 'ea=b': '0.33', 'be=d': '0.50', 'c=ba': '0.33', 'bd=a': '1.00', 'a=cb': '0.33', 'ea=c': '0.67', 'a=cd': '0.50', 'e=ac': '0.40', 'e=ab': '0.20', 'c=bd': '0.33', 'e=cb': '0.40', 'ed=b': '0.25', 'ed=c': '0.50'} cp = 0.5 if prune(d,cp) == d.keys(): print code not working :(( code not working :(( -- http://mail.python.org/mailman/listinfo/python-list
How to generate all permutations of a string?
Hi guys, I want to generate all permutations of a string. I've managed to generate all cyclic permutations. Please help :) def permute(string): l= [] l.append(string) string1 = '' for i in range(0,len(string)-1,1): string1 = string[1:len(string)] + string[:1] l.append(string1) string = string1 return l -- http://mail.python.org/mailman/listinfo/python-list
Re: How to generate all permutations of a string?
In article [EMAIL PROTECTED], Girish Sahani [EMAIL PROTECTED] wrote: I want to generate all permutations of a string. def permute(Seq) : generator which yields successive permutations of the elements of Seq. if len(Seq) == 0 : yield () else : for i in range(0, len(Seq)) : for rest in permute(Seq[:i] + Seq[i + 1:]) : yield (Seq[i],) + rest #end for #end for #end if #end permute thanks lawrence...however this func doesnt return the permuted strings, so i added some code as follows to generate a list of all the permutations: def permute(seq): l = [] if len(seq) == 0: yield [] else: for i in range(0,len(seq)): for rest in permute(seq[:i] + seq[i+1:]): yield (seq[i],) + rest for t in permute(seq): l.append(''.join(t)) return l This gives me a syntax error! I need to output a list that has all the permutations of the input string. -- http://mail.python.org/mailman/listinfo/python-list -- http://mail.python.org/mailman/listinfo/python-list
Re: How to generate all permutations of a string?
thanks lawrence...however this func doesnt return the permuted strings, so i added some code as follows to generate a list of all the permutations: def permute(seq): l = [] if len(seq) == 0: yield [] else: for i in range(0,len(seq)): for rest in permute(seq[:i] + seq[i+1:]): yield (seq[i],) + rest for t in permute(seq): l.append(''.join(t)) return l This gives me a syntax error! I need to output a list that has all the permutations of the input string. -- http://mail.python.org/mailman/listinfo/python-list p = [.join(s) for s in permute('abcdefg')] This fails to work too. I'm trying to call permute func from another func, python gives me a TypeError as follows: def permute(seq): l = [] if len(seq) == 0: yield [] else: for i in range(0,len(seq)): for rest in permute(seq[:i] + seq[i+1:]): yield (seq[i],) + rest def main(stringList): for string in stringList: permList = list(permute(string)) l = [.join(s) for s in permList] l = ['abc','abd','bcd'] main(l) TypeError: can only concatenate tuple (not list) to tuple -- James Stroud UCLA-DOE Institute for Genomics and Proteomics Box 951570 Los Angeles, CA 90095 http://www.jamesstroud.com/ -- http://mail.python.org/mailman/listinfo/python-list -- http://mail.python.org/mailman/listinfo/python-list
How to truncate/round-off decimal numbers?
Hi, I want to truncate every number to 2 digits after the decimal point. I tried the following but it doesnt work. a = 2 b = 3 round(a*1.0 / b,2) 0.67004 Inspite of specifying 2 in 2nd attribute of round, it outputs all the digits after decimal. -- http://mail.python.org/mailman/listinfo/python-list
Getting TypeError:list indices must be integers
Hi, Please check out the following loop,here indexList1 and indexList2 are a list of numbers. for index1 in indexList1: for index2 in indexList2: if ti1[index1] == ti2[index2] and not index1 != indexList1.pop(): index1+=1 index2+=1 continue elif index1 == indexList1.pop() and charList in pairList: k = string2.index(char2[0]) instance = ti2(k) tiNew = ti1.append(instance) tiNewList.append(tiNew) else: break On running my program, python gives me a TypeError saying: if ti1[index1] == ti2[index2] and not index1 != indexList1.pop(): TypeError: list indices must be integers Even though index1 and index2 are really integers.Please help! -- http://mail.python.org/mailman/listinfo/python-list
Getting TypeError:list indices must be integers: apology
Hi ppl, I'm really sorry for the previous post. I write mails very quickly and end up making errors in it. This time i ended up giving a code portion from an old copy of my program. Here's the code portion that is giving a TypeError:list indices must be integers for index1 in indexList1: for index2 in indexList2: if ti1[index1] == ti2[index2] and index1 != indexList1[-1]: index1+=1 index2+=1 continue elif index1 == indexList1[-1] and charList in pairList: k = string2.index(char2[0]) instance = ti2(k) tiNew = ti1.append(instance) tiNewList.append(tiNew) else: break -- http://mail.python.org/mailman/listinfo/python-list
Re: Getting TypeError:list indices must be integers
On 13/06/2006 4:11 PM, Girish Sahani wrote: [snip] instance = ti2(k) tiNew = ti1.append(instance) ti2 is quacking function but ti1 is quacking list. Possibilities: (1) You meant to type ti2[k] ... and this section of code has not yet been executed, and would have featured as episode N+1 had morbid curiosity not led me to read further. That is corrected. I'm appending a particular element of ti2 to ti1. It hasnt been executed because i'm stuck on that TypeError since 2 hours :( (2) You have the weirdest system of choosing names that I have seen for decades. :(( (3) Both of the above. Cheers, John -- http://mail.python.org/mailman/listinfo/python-list
Function to remove elements from a list not working
Hi, I am trying to convert a list of pairs (l4) to list l5 by removing those pairs from l4 which are not present in a third list called pairList. The following is a simplified part of the routine i have written. However it does not give the correct output. Please help! Its possible i have made a trivial mistke since i am a newbie. def getl5(): l5 = [] pairList = [[1,2],[3,4],[3,5],[3,6],[9,7],[8,9],[8,7],[7,9],[11,10]] l4 = [[4,2],[4,7],[4,10],[4,12],[9,2],[9,7],[9,10],[9,12],[11,2],[11,7]] for pair in l4: if pair not in pairList: element.remove(l4) l5.append(element) print l5 is,l5 -- http://mail.python.org/mailman/listinfo/python-list
Function to remove elements from a list not working (corrected)
Hi, I am trying to modify a list of pairs (l4) by removing those pairs which are not present in a third list called pairList. The following is a simplified part of the routine i have written. However it does not give the correct output. Please help! Its possible i have made a trivial mistke since i am a newbie. def getl5(): l5 = [] pairList = [[1,2],[3,4],[3,5],[3,6],[9,7],[8,9],[8,7],[7,9],[11,10]] l4 = [[4,2],[4,7],[4,10],[4,12],[9,2],[9,7],[9,10],[9,12],[11,2],[11,7]] for pair in l4: if pair not in pairList: l4.remove(pair) print l4 is,l4 The output given is: l4 is [[4, 7], [4, 12], [9, 7], [9, 12], [11, 7]] -- http://mail.python.org/mailman/listinfo/python-list
Re: Function to remove elements from a list not working (corrected)
Thanks!It workedi wasted a lot of time trying to find a bug in my code...what is wrong in iterating over a list and modifying it? Doesnt python take the modified list every time the loop starts? Also in this case, i want to add a condition that if none of the pairs are in pairList, element = []. How can i do that? Le Lundi 12 Juin 2006 10:25, Girish Sahani a écrit : Hi, I am trying to modify a list of pairs (l4) by removing those pairs which are not present in a third list called pairList. The following is a simplified part of the routine i have written. However it does not give the correct output. Please help! Its possible i have made a trivial mistke since i am a newbie. def getl5(): l5 = [] pairList = [[1,2],[3,4],[3,5],[3,6],[9,7],[8,9],[8,7],[7,9],[11,10]] l4 [[4,2],[4,7],[4,10],[4,12],[9,2],[9,7],[9,10],[9,12],[11,2],[11,7]] for pair in l4: if pair not in pairList: l4.remove(pair) print l4 is,l4 You are trying to modify a list while iterating over it, never do that ! The output given is: l4 is [[4, 7], [4, 12], [9, 7], [9, 12], [11, 7]] Is this work in your case ? def getl5(): pairList = [[1,2],[3,4],[3,5],[3,6],[9,7],[8,9],[8,7],[7,9],[11,10]] l4 = [[4,2],[4,7],[4,10],[4,12],[9,2],[9,7],[9,10],[9,12],[11,2],[11,7]] l4 = [ e for e in l4 if e in pairList ] print l5 is, l4 -- _ Maric Michaud _ Aristote - www.aristote.info 3 place des tapis 69004 Lyon Tel: +33 426 880 097 -- http://mail.python.org/mailman/listinfo/python-list -- http://mail.python.org/mailman/listinfo/python-list
Re: Function to remove elements from a list not working (corrected)
Thank you Markthis works too... Btw going slightly off-topic, when i try to run a code like below with around 50 elements (pairs) in l4,python just hangs. Any ideas why this is happening...the data is not that large :(( Girish Sahani wrote: Hi, I am trying to modify a list of pairs (l4) by removing those pairs which are not present in a third list called pairList. The following is a simplified part of the routine i have written. However it does not give the correct output. Please help! Its possible i have made a trivial mistke since i am a newbie. def getl5(): l5 = [] pairList = [[1,2],[3,4],[3,5],[3,6],[9,7],[8,9],[8,7],[7,9],[11,10]] l4 = [[4,2],[4,7],[4,10],[4,12],[9,2],[9,7],[9,10],[9,12],[11,2],[11,7]] for pair in l4: if pair not in pairList: l4.remove(pair) print l4 is,l4 The output given is: l4 is [[4, 7], [4, 12], [9, 7], [9, 12], [11, 7]] It is better to iterate over a copy, e.g. like this: pairList = [[1,2],[3,4],[3,5],[3,6],[9,7],[8,9],[8,7],[7,9],[11,10]] l4 = [[4,2],[4,7],[4,10],[4,12],[9,2],[9,7],[9,10],[9,12],[11,2],[11,7]] for pair in l4[:]: if pair not in pairList: l4.remove(pair) print l4 is,l4 -- http://mail.python.org/mailman/listinfo/python-list -- http://mail.python.org/mailman/listinfo/python-list
Re: Function to remove elements from a list working, but python hangs :((
Hey Bruno...you are seeing the wrong post :P...please ignore this and check out the one with (corrected) appended at the end... Also, i used the list comprehension thingy which u have given, but now the problem is python just hangs if my list l4 contains around 50 pairs...considering its not that big a data, this shouldnt happen Should it?? Girish Sahani wrote: Hi, I am trying to convert a list of pairs (l4) to list l5 by removing those pairs from l4 which are not present in a third list called pairList. The following is a simplified part of the routine i have written. However it does not give the correct output. Please help! Its possible i have made a trivial mistke since i am a newbie. def getl5(): ot Please avoid this kind of names : it's both meaningless (what the ... is 'l5', I wonder ???) and confusing (is it 'l5' or '15' ?) FWIW, read this: http://mindprod.com/jgloss/unmain.html /ot l5 = [] pairList = [[1,2],[3,4],[3,5],[3,6],[9,7],[8,9],[8,7],[7,9],[11,10]] From a semantic POV, you should use tuples for pairs - not lists. l4 = [[4,2],[4,7],[4,10],[4,12],[9,2],[9,7],[9,10],[9,12],[11,2],[11,7]] for pair in l4: if pair not in pairList: err... see below... element.remove(l4) l5.append(element) This is outside the for loop, so this would be executed only once (if the rest of the code was correct, of course...) print l5 is,l5 You did not test this code, did you ? getl5() Traceback (most recent call last): File stdin, line 1, in ? File /usr/tmp/python-961l_S.py, line 7, in getl5 NameError: global name 'element' is not defined The SimpleStupid(tm) way to do this is far more simple - at least if I understood your specifications: pairList = [(1,2),(3,4),(3,5),(3,6),(9,7),(8,9),(8,7),(7,9),(11,10)] l4 = [(4,2),(4,7),(4,10),(4,12),(9,2),(9,7),(9,10),(9,12),(11,2),(11,7)] l5 = [pair for pair in l4 if pair in pairList] print l5 is : , l5 Now this is not necessarily the most efficient solution... -- bruno desthuilliers python -c print '@'.join(['.'.join([w[::-1] for w in p.split('.')]) for p in '[EMAIL PROTECTED]'.split('@')]) -- http://mail.python.org/mailman/listinfo/python-list -- http://mail.python.org/mailman/listinfo/python-list
Re: Function to remove elements from a list working, but Python Hangs :((
I guess it's already the case with lst = [ e for e in lst if e in pairs ] In [1]: lst = (1,2,3,4) In [2]: pairs=(5,) In [3]: lst=[e for e in lst if e in pairs] In [4]: lst Out[4]: [] Yes its working. I realized that i was giving an input which didnt satisfy the condition. Now if you could please check out this new problem i'm facing,related to this.In my code,l4 is a list of lists, and these lists contain such pairs. e.g. l4 = [[[4,2],[4,7],[4,10],[4,12],[9,2],[9,7],[9,10],[9,12],[11,2],[11,10]],[[1,2],[3,5],[7,8]]] which is a list of 2 lists (having 13 pairs) Now when l4 has 50 pairs, and i run the code, python just hangs, and i'm quite sure its because of this. Is there any way to rectify this??? -- _ Maric Michaud _ Aristote - www.aristote.info 3 place des tapis 69004 Lyon Tel: +33 426 880 097 -- http://mail.python.org/mailman/listinfo/python-list -- http://mail.python.org/mailman/listinfo/python-list
Error in Chain of Function calls
Hi, There is a code in my main function which is something like: while prunedFinal != []: prunedNew = genColocations(prunedK) *** tableInstancesNew = genTableInstances(prunedNew,tableInstancesK) tiCountDict = tiCount(tableInstancesNew) tiDict = findPI(tableInstancesNew) prunedFinal = pruneTI(tiDict,pi) rulesDict = genRules(prunedFinal) cpDict = findCP(rulesDict) prunedRulesList = pruneCP(cpDict,cp) prunedK = prunedFinal tableInstancesK = tableInstancesNew else: return prunedRulesList prunedK and tableInstancesK are defined in the main function. Before the main function, i have defined the other functions such as genColocations,genTableInstances,etc. Output of genColocations is to be given to the next function genTableInstances,output of this function to tiCount and findPI, and so on. However i am getting an error at the line marked with ***. Also,i am getting a ValueError in the code below: for s in prunedNew: substrings = [s[:i]+s[i+1:] for i in range(len(s))] for string in substrings: if string not in prunedK: prunedNew.remove(s) continue continue The error is: prunedNew.remove(s) ValueError: list.remove(x): x not in list Could anyone enlighten me as to why i'm getting these two errors?? Thanks a lot, girish -- http://mail.python.org/mailman/listinfo/python-list
Re: Error in Chain of Function calls
Girish Sahani wrote: However i am getting an error at the line marked with ***. what error ? ...line 266, in colocationMiner prunedNew = genColocations(prunedK) genColocations is a function defined before which returns prunedNew. Also,i am getting a ValueError in the code below: for s in prunedNew: substrings = [s[:i]+s[i+1:] for i in range(len(s))] for string in substrings: if string not in prunedK: prunedNew.remove(s) continue continue The error is: prunedNew.remove(s) ValueError: list.remove(x): x not in list the ValueError means exactly what it says -- have you verified that the value of s really is present in the list? did you really mean to remove s and not, say, string ? Yes. I want to remove s from the prunedNew list if that condition is not satisfied. /F -- http://mail.python.org/mailman/listinfo/python-list -- http://mail.python.org/mailman/listinfo/python-list
Re: Error in Chain of Function calls (Code Attached)
Girish Sahani wrote: Hi, There is a code in my main function which is something like: while prunedFinal != []: prunedNew = genColocations(prunedK) *** tableInstancesNew = genTableInstances(prunedNew,tableInstancesK) tiCountDict = tiCount(tableInstancesNew) tiDict = findPI(tableInstancesNew) prunedFinal = pruneTI(tiDict,pi) rulesDict = genRules(prunedFinal) cpDict = findCP(rulesDict) prunedRulesList = pruneCP(cpDict,cp) prunedK = prunedFinal tableInstancesK = tableInstancesNew else: return prunedRulesList prunedK and tableInstancesK are defined in the main function. defined as what ? functions, strings, lists, classes, ... ? PrunedK is a list that contains 2 length strings and tableInstancesK is a dictionary,its keys are 2 length strings and values are lists of lists Before the main function, i have defined the other functions such as genColocations,genTableInstances,etc. Output of genColocations is to be given to the next function genTableInstances,output of this function to tiCount and findPI, and so on. However i am getting an error at the line marked with ***. Which error ? How do you hope us to be of any help here if you don't *at least* provide the full traceback ? FWIW, the canonical way to do things is to: - provide minimal *runnable* code exposing the problem - explain what you hoped to get - explain what you got instead (including full traceback) As a matter of fact, it's often the case that one solves the problem when working on the first point !-) (snip) Ohh...I was thinking that posting the whole code would not be a good idea. The error i get above is: line 266, in colocationMiner prunedNew = genColocations(prunedK) Anyways, i've attached the file colocations.py. The expected output is a List of rules (prunedRulesList).These rules are themselves lists.e.g ['ab','c'] denotes the rule ab=c. Please do have a look if you have time :). -- bruno desthuilliers python -c print '@'.join(['.'.join([w[::-1] for w in p.split('.')]) for p in '[EMAIL PROTECTED]'.split('@')]) -- http://mail.python.org/mailman/listinfo/python-list #convert the input file into a dictionary (global ids mapped to feature type) and a list of global id pairs def get_colocations(filename): lines = open(filename).read().split(\n) colocnDict = {} for line in lines: n1, b1, n2, b2 = line.split(,) n1 = int(n1) n2 = int(n2) a1 = b1.strip(') a2 = b2.strip(') colocnDict[n1] = a1 colocnDict[n2] = a2 return colocnDict #get pairs of feature ids which are colocated def getPairs(filename): lines = open(filename).read().split(\n) pairList = [] for line in lines: n1, b1, n2, b2 = line.split(,) pairList.append([n1, n2]) return pairList #count number of occurences of each feature in the feature list and store in featueCountDict def getFeatureCount(): colocnDict = get_colocations(colocations.txt) featureList= colocnDict.values() featureListUnique = [] [featureListUnique.append(word) for word in featureList if not featureListUnique.count(word)] featureCountDict = {} for feature in featureListUnique: featureCountDict[feature] = featureList.count(feature) return featureCountDict def k2k1(string1, string2): for c in string1: string2 = string2.replace(c,,1) if len(string2) == 1: string1 += string2 else: pass return string1 def dictInvert(d): dictInv = {} for k, v in d.iteritems(): keys = dictInv.setdefault(v, []) keys.append(k) return dictInv #Generate candidate co-locations of size k+1 from size k #get lower level subsets and prune it by antimonotone property def genColocations(prunedK): prunedNew = substringList = [] for string1 in prunedK: for string2 in prunedK: k = len(string2) if string1 != string2: string1 = k2k1(string1, string2) if len(string1) == k+1: prunedNew.append(string1) for s in prunedNew: substrings = [s[:i]+s[i+1:] for i in range(len(s))] for string in substrings: if string not in prunedK: prunedNew.remove(s) continue continue #tableInstancesNew is a dictionary with keys as k level colocations and values as table instances def genTableInstances(prunedNew,tableInstancesK): colocnDict = get_colocations(colocations.txt) tableInstancesNew = {} for s in p: substring1 = s[:len(s)-1] substring2 = s[:len(s)-2]+s[len(s)-1:] #get 2 substrings list1 = tableInstancesK(substring1) list2
Re: Error in Chain of Function calls (Code Attached)
Thanks for the help borisi'll try using sets instead of lists But i dont understand the point below...full traceback means what :-? Girish Sahani wrote: However i am getting an error at the line marked with ***. Which error ? How do you hope us to be of any help here if you don't *at least* provide the full traceback ? FWIW, the canonical way to do things is to: - provide minimal *runnable* code exposing the problem - explain what you hoped to get - explain what you got instead (including full traceback) As a matter of fact, it's often the case that one solves the problem when working on the first point !-) (snip) Ohh...I was thinking that posting the whole code would not be a good idea. And what changed your mind ? Please note the word minimal in bruno's sentence about submitting your code. And also the request for explanations. The error i get above is: line 266, in colocationMiner prunedNew = genColocations(prunedK) OTOH, bruno asked for the full traceback, that's not it - that's not the error in any constructive sense, neither its type nor its real location. -- http://mail.python.org/mailman/listinfo/python-list -- http://mail.python.org/mailman/listinfo/python-list
Re: How to generate k+1 length strings from a list of k lengthstrings?
Yes it is the former of course.Common elements could be in any order in both strings. Thanks Marc :)...though we need to add an if for checking the length of the string (k+1). Jon Clements wrote: Are you asking the question, Which pairs of strings have one character different in each?, or Which pairs of strings have a substring of len(string) - 1 in common?. Jon. I imagine it's the former because the latter is trivially easy, I mean _really_ trivially easy. -- http://mail.python.org/mailman/listinfo/python-list -- http://mail.python.org/mailman/listinfo/python-list
Re: Reading from a file and converting it into a list of lines
On 6/06/2006 4:15 PM, Girish Sahani wrote: Really sorry for that indentation thing :) I tried out the code you have given, and also the one sreeram had written. In all of these,i get the same error of this type: Error i get in Sreeram's code is: n1,_,n2,_ = line.split(',') ValueError: need more than 1 value to unpack And error i get in your code is: for n1, a1, n2, a2 in reader: ValueError: need more than 0 values to unpack Any ideas why this is happening? In the case of my code, this is consistent with the line being empty, probably the last line. As my mentor Bruno D. would say, your test data does not match your spec :-) Which do you want to change, the spec or the data? Thanks John, i just changed my Data file so as not to contain any empty lines, i guess that was the easier solution ;) You can change my csv-reading code to detect dodgy data like this (for example): for row in reader: if not row: continue # ignore empty lines, wherever they appear if len(row) != 4: raise ValueError(Malformed row %r % row) n1, a1, n2, a2 = row In the case of Sreeram's code, perhaps you could try inserting print line = , repr(line) before the statement that is causing the error. Thanks a lot, girish -- http://mail.python.org/mailman/listinfo/python-list -- http://mail.python.org/mailman/listinfo/python-list
How to generate k+1 length strings from a list of k length strings?
I have a list of strings all of length k. For every pair of k length strings which have k-1 characters in common, i want to generate a k+1 length string(the k-1 common characters + 2 not common characters). e.g i want to join 'abcd' with bcde' to get 'abcde' but i dont want to join 'abcd' with 'cdef' Currently i'm joining every 2 strings, then removing duplicate characters from every joined string and finally removing all those strings whose length != k+1.Here's the code i've written: for i in range(0,len(prunedK) - 1,1): if k in range(1,len(prunedK),1) i+k = len(prunedK) -1: colocn = prunedK[i] + prunedK[i+k] prunedNew1.append(colocn) continue for string in prunedNew1: stringNew = withoutDup(string) prunedNew.append(stringNew) continue But this one is quite bad in the time aspect :(. Thanks in advance, girish -- http://mail.python.org/mailman/listinfo/python-list
Re: Reading from a file and converting it into a list of lines
Really sorry for that indentation thing :) I tried out the code you have given, and also the one sreeram had written. In all of these,i get the same error of this type: Error i get in Sreeram's code is: n1,_,n2,_ = line.split(',') ValueError: need more than 1 value to unpack And error i get in your code is: for n1, a1, n2, a2 in reader: ValueError: need more than 0 values to unpack Any ideas why this is happening? Thanks a lot, girish -- http://mail.python.org/mailman/listinfo/python-list
Concatenating dictionary values and keys, and further operations
I wrote the following code to concatenate every 2 keys of a dictionary and their corresponding values. e.g if i have tiDict1 = tiDict1 = {'a':[1,2],'b':[3,4,5]} i should get tiDict2={'ab':[1,2][3,4,5]} and similarly for dicts with larger no. of features. Now i want to check each pair to see if they are connected...element of this pair will be one from the first list and one from the seconde.g for 'ab' i want to check if 1 and 3 are connected,then 1 and 4,then 1 and 5,then 2 and 3,then 2 and 4,then 2 and 5. The information of this connected thing is in a text file as follows: 1,'a',2,'b' 3,'a',5,'a' 3,'a',6,'a' 3,'a',7,'b' 8,'a',7,'b' . . This means 1(type 'a') and 2(type 'b') are connected,3 and 5 are connected and so on. I am not able to figure out how to do this.Any pointers would be helpful Here is the code i have written till now: [code] def genTI(tiDict): tiDict1 = {} tiList = [tiDict1.keys(),tiDict1.values()] length =len(tiDict1.keys())-1 for i in range(0,length,1): for j in range(0,length,1): for k in range(1,length+1,1): if j+k = length: key = tiList[i][j] + tiList[i][j+k] value = [tiList[i+1][j],tiList[i+1][j+k]] tiDict2[key] = value continue continue continue return tiDict2 [/code] Thanks in advance, girish -- http://mail.python.org/mailman/listinfo/python-list
How to search for substrings of a string in a list?
Given a length k string,i want to search for 2 substrings (overlap possible) in a list consisting of length k-1 strings. These 2 substrings when 'united' give the original string. e.g given 'abc' i want to search in the list of 2-length strings ['ab',ac','cd','bc','bd'] to extract either 1) 'ab and 'ac' OR ('a' common) 2) 'ab' and 'bc' OR ('b' common) 3) 'ac' and 'bc' ('c' common) In all these cases, one of the letter is common in the 2 strings. Out of the k-1 letters in each length k-1 string,k-2 will be common. Another e.g is: Given 'abcd' and list ['abc,'abd','bcd'],i must extract 1)abc and abd OR ('ab' common) 2)abc and bcd OR 3)abd and bcd OR Here 2 letters are common in all the solutions. I havent been able to figure out a method. Pleeez help!! Thanks in advance, girish -- http://mail.python.org/mailman/listinfo/python-list
How to generate all k-1 level substrings of a string?
I want to generate all substrings of size k-1 from a string of size k. e.g 'abcd' should give me ['abc','abd','bcd','acd'] Order of these strings in the list doesnt matter. Also order doesnt matter inside the string e.g 'abc' or 'bca' or 'bac' is the same. I wrote the following code but it doesnt give the full output: subsetList = [] for element in prunedNew: for i in range(0,2): subsetList.append(element[i:i+len(element)-1]) continue continue return prunedNew Thanks in Advance, girish -- http://mail.python.org/mailman/listinfo/python-list
Reading from a file and converting it into a list of lines: code not working
I have a text file in the following format: 1,'a',2,'b' 3,'a',5,'c' 3,'a',6,'c' 3,'a',7,'b' 8,'a',7,'b' . . . Now i need to generate 2 things by reading the file: 1) A dictionary with the numbers as keys and the letters as values. e.g the above would give me a dictionary like {1:'a', 2:'b', 3:'a', 5:'c', 6:'c' } 2) A list containing pairs of numbers from each line. The above formmat would give me the list as [[1,2],[3,5],[3,6][3,7][8,7]..] I wrote the following codes for both of these but the problem is that lines returns a list like [1,'a',2,'b',3,'a',5,'c,3,'a',6,'c'.] Now due to the around each line,it is treated like one object and i cannot access the elements of a line. [code] #code to generate the dictionary def get_colocations(filename): lines = open(filename).read().split(\n) colocnDict = {} i = 0 for line in lines: if i = 2: colocnDict[line[i]] = line[i+1] i+=2 continue return colocnDict [/code] [code] def genPairs(filename): lines = open(filename).read().split(\n) pairList = [] for line in lines: pair = [line[0],line[2]] pairList.append(pair) i+=2 continue return pairList [/code] Please help :(( -- http://mail.python.org/mailman/listinfo/python-list
Re: the most efficient method of adding elements to the list
Hi, Would it be .append()? Does it reallocate te list with each apend? Yes it does. If order of the new element being added doesnt matter you can use append. If it does, you can use insert(). l=[] for i in xrange(n): l.append(i) Thx, A. -- http://mail.python.org/mailman/listinfo/python-list -- http://mail.python.org/mailman/listinfo/python-list