Re: trouble with replace
>>> pats = ['abcdef', 'defgef', 'effwer'] >>> reps = ['highway', 'news', 'monitor'] >>> s = 'defgefabcdefy\n\n\n effwerbyuuuterrfr' >>> reduce(lambda x,y: x.replace(*y), zip(pats,reps), s) 'newshighwayy\n\n\n monitorbyuuuterrfr' f pemberton wrote: > I have a string (xdata) and theres a newline after every 17 characters > of the string. I was wondering how I can replace multiple substrings > multiple times within a string? To put it another way, this is what i > want to do. > > Substring to find ("abcdef") replace it with ("highway") > search again, substring to find ("defgef") replace it with > ("news").Search again and find ("effwer") replace it with > ("monitor").Example string to search under is '''defgefabcdefy > > > effwerbyuuuterrfr''' > > I've tried using replace but its not working for me. > xdata.replace('abcdef', 'highway') > xdata.replace('defgef', 'news') > xdata.replace('effwer', 'monitor') > Basically I would want to get the output of "newshighwayy > > >monitorbyuuuterrfr" > > Thanks, any help would be appreciated. > -- http://mail.python.org/mailman/listinfo/python-list
Re: Recurse Directories and process files in directory
Use os.system to execute a string and os.walk to get a recursive list of files >>> def processdir(curdir,subdirs,files): ... map(lambda f:os.system('\\cygwin\\bin\\wc -l "%s"' % f), [curdir+os.sep+x for x in files]) ... >>> map(lambda x:processdir(*x), os.walk('\\dev\qclient')); 6 \dev\qclient\.classpath 17 \dev\qclient\.project 774 \dev\qclient\bookmarks.html 8 \dev\qclient\kx\qclient\DirTree$1.class 20 \dev\qclient\kx\qclient\DirTree$2.class 17 \dev\qclient\kx\qclient\DirTree$3.class 8 \dev\qclient\kx\qclient\DirTree$4.class 18 \dev\qclient\kx\qclient\DirTree$5.class 9 \dev\qclient\kx\qclient\DirTree$CellRenderer.class 18 \dev\qclient\kx\qclient\DirTree$ServerPopupMenu.class 5 \dev\qclient\kx\qclient\DirTree$ServerTreeNode.class 28 \dev\qclient\kx\qclient\DirTree.class 148 \dev\qclient\kx\qclient\DirTree.java 11 \dev\qclient\kx\qclient\Q$Date.class 4 \dev\qclient\kx\qclient\Q$Dict.class 4 \dev\qclient\kx\qclient\Q$Flip.class 5 \dev\qclient\kx\qclient\Q$KException.class 12 \dev\qclient\kx\qclient\Q$Minute.class 12 \dev\qclient\kx\qclient\Q$Month.class 15 \dev\qclient\kx\qclient\Q$Second.class 18 \dev\qclient\kx\qclient\Q$Time.class 147 \dev\qclient\kx\qclient\Q.class 137 \dev\qclient\kx\qclient\Q.java 8 \dev\qclient\kx\qclient\QClient$1.class 11 \dev\qclient\kx\qclient\QClient$2.class 15 \dev\qclient\kx\qclient\QClient$3.class 9 \dev\qclient\kx\qclient\QClient$4.class 85 \dev\qclient\kx\qclient\QClient.class 185 \dev\qclient\kx\qclient\QClient.java 4 \dev\qclient\kx\qclient\QServer$DefaultListener.class 3 \dev\qclient\kx\qclient\QServer$Listener.class 28 \dev\qclient\kx\qclient\QServer.class 50 \dev\qclient\kx\qclient\QServer.java 59 \dev\qclient\kx\qclient\ServerDialog.class 146 \dev\qclient\kx\qclient\ServerDialog.java 14 \dev\qclient\kx\qclient\ServerDisplay$1.class 18 \dev\qclient\kx\qclient\ServerDisplay$StatusBar.class 19 \dev\qclient\kx\qclient\ServerDisplay.class 78 \dev\qclient\kx\qclient\ServerDisplay.java [None, None, None] >>> KraftDiner wrote: > Hi I need help writing a python script that traverses (recursivly) a > directory and its sub directories and processes all files in the > directory. So at each directory if there are files in it I must build > a list of those files and process them by exectuing a system command > (exec?) > > Can some one tell me what methods to use to: > a) Walk the directory tree > b) execute a system command with parameters. > > TIA. > -- http://mail.python.org/mailman/listinfo/python-list
Re: yet another noob question
def pr(x): print(x) >>> x=map(pr,[x for x in xrange(11,56) if '1'<=min(str(x)) and >>> max(str(x))<='5']) 11 12 13 14 15 21 22 23 24 25 31 32 33 34 35 41 42 43 44 45 51 52 53 54 55 mike_wilson1333 wrote: > I would like to generate every unique combination of numbers 1-5 in a 5 > digit number and follow each combo with a newline. So i'm looking at > generating combinations such as: (12345) , (12235), (4) and so on. > What would be the best way to do this? So, basically i'm looking for a > list of all combinations of 1-5 in a 5 digit unique number. Also, when > I send the list to print there can't be any duplicates of the combos. > > > Thanks, > > Mike > -- http://mail.python.org/mailman/listinfo/python-list
Re: yet another noob question
Or without filter: from operator import add def pr(x): print x def cross(x,y): return reduce(add, [[a+b for b in y] for a in x]) x=map(pr, reduce(cross, [map(str,range(1,6))]*5)) mike_wilson1333 wrote: > I would like to generate every unique combination of numbers 1-5 in a 5 > digit number and follow each combo with a newline. So i'm looking at > generating combinations such as: (12345) , (12235), (4) and so on. > What would be the best way to do this? So, basically i'm looking for a > list of all combinations of 1-5 in a 5 digit unique number. Also, when > I send the list to print there can't be any duplicates of the combos. > > > Thanks, > > Mike > -- http://mail.python.org/mailman/listinfo/python-list
Re: yet another noob question
better (sorry, still learning Python): def cross(x,y): return [a+b for a in x for y in b] Jason Nordwick wrote: > Or without filter: > > from operator import add > def pr(x): print x > def cross(x,y): return reduce(add, [[a+b for b in y] for a in x]) > x=map(pr, reduce(cross, [map(str,range(1,6))]*5)) > > > > mike_wilson1333 wrote: >> I would like to generate every unique combination of numbers 1-5 in a 5 >> digit number and follow each combo with a newline. So i'm looking at >> generating combinations such as: (12345) , (12235), (4) and so on. >> What would be the best way to do this? So, basically i'm looking for a >> list of all combinations of 1-5 in a 5 digit unique number. Also, when >> I send the list to print there can't be any duplicates of the combos. >> >> >> Thanks, >> >> Mike >> > -- http://mail.python.org/mailman/listinfo/python-list
Re: yet another noob question
Somehow my other response to the list got lost. I'm still learning Python, but this seems much better than my first attempt: def pr(x): print x def cross(x,y): return [a+b for a in x for b in y] x=map(pr, reduce(cross, [map(str,range(1,6))]*5)) -j Stargaming wrote: > Jason Nordwick schrieb: >> Or without filter: >> >> from operator import add >> def pr(x): print x >> def cross(x,y): return reduce(add, [[a+b for b in y] for a in x]) >> x=map(pr, reduce(cross, [map(str,range(1,6))]*5)) >> > [...] > > reduce(add, list) is the same as sum(list) and is only half as fast as sum: > >>> from timeit import Timer > >>> sum(Timer("sum(range(500))").repeat(200, 1000))/200 > 0.055693786500798058 > >>> sum(Timer("reduce(add, range(500))", "from operator import > add").repeat(200, 1000))/200 > 0.10820861031220445 > >>> > > Also note that reduce will be removed in Python 3000. -- http://mail.python.org/mailman/listinfo/python-list
Re: yet another noob question
Stargaming wrote: > > Also note that reduce will be removed in Python 3000. What will replace it? -j -- http://mail.python.org/mailman/listinfo/python-list
Re: yet another noob question
*mouth agape* Wow. That really sucks. I make extensive use of reduce. It seems to run more than twice as fast as a for loop. >>> t = Timer('bino.reduceadd(bino.bits)', 'import bino') >>> s = Timer('bino.loopadd(bino.bits)', 'import bino') >>> t.timeit(10) 1.2373670396656564 >>> s.timeit(10) 2.6450051612705039 >>> t.timeit(100) 11.312374896809501 >>> s.timeit(100) 25.817132345032689 where bits = map(lambda x:randint(0,1), xrange(100)) def reduceadd(v): return reduce(add, v) def loopadd(v): sum = 0 for x in v: sum = add(sum, x) return sum (Yes, I know there are better ways to sum up a list, but I just wanted to test the performance of the loop against reduce. In most of my reduce usages, the function passed to reduce is much more complex.) -j [EMAIL PROTECTED] wrote: > Jason Nordwick: >> Stargaming wrote: >>> Also note that reduce will be removed in Python 3000. >> What will replace it? > > Nothing, I presume. You will have to write a function to find another > way to solve problems. > > Bye, > bearophile > -- http://mail.python.org/mailman/listinfo/python-list
Re: yet another noob question
I use reduce to also do indexing, hashing with upsert semantics of lists of key-value pairs, transitioning through a state table, etc... Somebody else pointed out to me how odd it is of Python to be ditching reduce when Guido van Rossum was hired by Google, and Google is literally built on map and reduce (http://en.wikipedia.org/wiki/Mapreduce). Here is a code fragment I pulled from what I am currently working on (with a little modification to make things simple): First build a tree: >>> def byn(a,n): return [[a[i] for i in x] for x in zip(*[range(y,len(a),n) >>> for y in range(n)])] ... >>> def whilep(pred,f,x): ... while pred(x): x = f(x) ... return x ... >>> t = whilep(lambda x:1>> >>> t ['a', 'b'], ['c', 'd']], [['e', 'f'], ['g', 'h']]], [[['i', 'j'], ['k', 'l']], [['m', 'n'], ['o', 'p'] Now if I had the list [0,1,0,0,1] and needed to find what letter it encodes: >>> t[0][1][0][0][1] 'j' But sometimes, I want to be able to index branches or the leaves are at different depths, so I essentially have a list of indexes to follow: >>> def indexDepth(a,i): return reduce(lambda x,y: x[y], i, a) ... >>> indexDepth(t,[0,1,0,0,1]) 'j' Also, I completely understand your timings, but you are not timing reduce against a hand coded loop, but instead the operator '+' against the function add, as the function symbol lookup and call seem to have a heavy price. Reduce was one of the nice ways to eliminate some of those lookups. -j [EMAIL PROTECTED] wrote: > Jason Nordwick wrote: >> *mouth agape* >> >> Wow. That really sucks. I make extensive use of reduce. It seems to run more >> than twice as fast as a for loop. >> >>>>> t = Timer('bino.reduceadd(bino.bits)', 'import bino') >>>>> s = Timer('bino.loopadd(bino.bits)', 'import bino') >>>>> t.timeit(10) >> 1.2373670396656564 >>>>> s.timeit(10) >> 2.6450051612705039 >>>>> t.timeit(100) >> 11.312374896809501 >>>>> s.timeit(100) >> 25.817132345032689 >> >> where >> >> bits = map(lambda x:randint(0,1), xrange(100)) >> >> def reduceadd(v): >> return reduce(add, v) >> >> def loopadd(v): >> sum = 0 >> for x in v: >> sum = add(sum, x) >> return sum >> >> >> >> (Yes, I know there are better ways to sum up a list, but I just wanted to >> test the performance of the loop against reduce. In most of my reduce >> usages, the function passed to reduce is much more complex.) > [...] > > Assuming Guido van Rossum is right and reduce() is only useful for > arithmetic expressions (could you show an example where you use it > else?), you could take advantage of more "built-ins". > To spoiler the result of this, the loop-variant is slightly (in this > test: 2ms) faster. > The results on my amd64 3k+ were: > Reduce: 0.5686828074520.56633643382 > For-loop: 0.56633643382 > > #!/usr/bin/env python > # > # > > from random import randint > from timeit import Timer > from operator import add > > def bits(): > return [randint(0,1) for x in xrange(1000)] > # use def & list comprehension since lambda/map will be removed in > Python 3.0, too > > > print bits()[0:5] > print bits()[0:5] # it's working. > > def reducefunc(x): > return reduce(add,x) > def loopfunc(x): > y = 0 > for i in x: y += i > return y > > x = bits() > print reducefunc(x) > print loopfunc(x) # both give proper output > > print sum(Timer("reducefunc(bits())", "from __main__ import bits, > reducefunc").repeat(20,100))/20 > print sum(Timer("loopfunc(bits())", "from __main__ import bits, > loopfunc").repeat(20,100))/20 > -- http://mail.python.org/mailman/listinfo/python-list
Re: yet another noob question
That isn't what I meant. If there was a a point (and I'm not really sure that I'm even trying to make one), the point was that Google makes heavy use of reduce-like functionality, essentially implementing a distributed reduce across a cluster. From what I hear, they use a lot of Python and hired van Rossum for a reason. It just seems odd (don't read anything into this than mere cocked eyebrows) that the language designer that they hired and obviously have a lot of trust in would decide that reduce was essentially pointless. Google's distributed reduce seems to point in opposite way. However, if reduce could be rolled into the list comprehension syntax, that would be even better. Or take it that extra step and roll a grouping functionality in there too, then you would have map, reduce, group, and filter all in one construct. You could call it select (joins are merely indexes into other structures). -j Steve Holden wrote: > Jason Nordwick wrote: >> I use reduce to also do indexing, hashing with upsert semantics of lists of >> key-value pairs, transitioning through a state table, etc... >> >> Somebody else pointed out to me how odd it is of Python to be ditching >> reduce when Guido van Rossum was hired by Google, and Google is literally >> built on map and reduce (http://en.wikipedia.org/wiki/Mapreduce). >> > > That seems a bit literal. Just because they use a tool called MapReduce > that doesn't imply that thay chose to implement it with the Python map() > and reduce() functions. It's a distributed application, in case you > hadn't noticed ... > > regards > Steve -- http://mail.python.org/mailman/listinfo/python-list
getting database column names from query
I'm using MySQLdb and can connect and issue queries that return result sets, but I how do I get the column names for those result sets? >>> c = MySQLdb.connect(*creds) >>> k = c.cursor() >>> k.execute("select * from account") 3L >>> k.fetchall() ((1L, 'test', -1L), (2L, 'Test', -1L), (3L, 'Test2', -1L)) -j -- http://mail.python.org/mailman/listinfo/python-list