Home

zhangyiqun

Thoughts, stories and ideas.

2014年以前 About Feed

28 Dec 2009
python技巧一则:某些情况下str.join比+=效率高

If s and t are both strings, some Python implementations such as CPython can usually perform an in-place optimization for assignments of the form s = s + t or s += t. When applicable, this optimization makes quadratic run-time much less likely. This optimization is both version and implementation dependent. For performance sensitive code, it is preferable to use the str.join() method which assures consistent linear concatenation performance across versions and implementations.

文中的s和t是什么?

There are six sequence types: strings, Unicode strings, lists, tuples, buffers, and xrange objects.

For other containers see the built in dict and set classes, and the collections module.

代码片段。不言自明。

   for root, dirs, files in os.walk('/media/cdrom0'):
      export+="\n %s;%s;%s" % (root,dirs,files)
   open('TokyoHot1', 'w').write(export)
   for root, dirs, files in os.walk('/media/cdrom0'):
        export.append("\n %s;%s;%s" % (root,dirs,files))
    open('TokyoHot2', 'w').write(''.join(export))

zhangyiqun

scribble

2014年以前 About Feed