48

本着现有的“你最有用的 C/C++ 片段是什么”的精神- 线程:

你们有没有(经常)使用的简短的、单功能的 Python 代码片段并想与 StackOverlow 社区分享?请尽量减少条目(可能少于 25 行?),并且每篇文章只提供一个示例。

我将从一个我不时使用的简短片段开始计算 python 项目中的 sloc (源代码行):

# prints recursive count of lines of python source code from current directory
# includes an ignore_list. also prints total sloc

import os
cur_path = os.getcwd()
ignore_set = set(["__init__.py", "count_sourcelines.py"])

loclist = []

for pydir, _, pyfiles in os.walk(cur_path):
    for pyfile in pyfiles:
        if pyfile.endswith(".py") and pyfile not in ignore_set:
            totalpath = os.path.join(pydir, pyfile)
            loclist.append( ( len(open(totalpath, "r").read().splitlines()),
                               totalpath.split(cur_path)[1]) )

for linenumbercount, filename in loclist: 
    print "%05d lines in %s" % (linenumbercount, filename)

print "\nTotal: %s lines (%s)" %(sum([x[0] for x in loclist]), cur_path)
4

22 回答 22

37

我喜欢使用any和生成器:

if any(pred(x.item) for x in sequence):
    ...

而不是这样编写的代码:

found = False
for x in sequence:
    if pred(x.n):
        found = True
if found:
    ...

我首先从 Peter Norvig 的一篇文章中了解到这种技术。

于 2009-03-29T05:57:20.420 回答
23

初始化二维列表

虽然这可以安全地初始化列表:

lst = [0] * 3

相同的技巧不适用于 2D 列表(列表列表):

>>> lst_2d = [[0] * 3] * 3
>>> lst_2d
[[0, 0, 0], [0, 0, 0], [0, 0, 0]]
>>> lst_2d[0][0] = 5
>>> lst_2d
[[5, 0, 0], [5, 0, 0], [5, 0, 0]]

运算符 * 复制其操作数,并且用 [] 构造的复制列表指向同一个列表。正确的方法是:

>>> lst_2d = [[0] * 3 for i in xrange(3)]
>>> lst_2d
[[0, 0, 0], [0, 0, 0], [0, 0, 0]]
>>> lst_2d[0][0] = 5
>>> lst_2d
[[5, 0, 0], [0, 0, 0], [0, 0, 0]]
于 2009-03-28T08:35:01.780 回答
22

我知道的唯一让我惊叹的“技巧”就是枚举。它允许您访问 for 循环中元素的索引。

>>> l = ['a','b','c','d','e','f']
>>> for (index,value) in enumerate(l):
...     print index, value
... 
0 a
1 b
2 c
3 d
4 e
5 f
于 2009-03-28T02:16:45.340 回答
18

zip(*iterable)转置一个可迭代的。

>>> a=[[1,2,3],[4,5,6]]
>>> zip(*a)
    [(1, 4), (2, 5), (3, 6)]

它对字典也很有用。

>>> d={"a":1,"b":2,"c":3}
>>> zip(*d.iteritems())
[('a', 'c', 'b'), (1, 3, 2)]
于 2009-03-28T18:11:56.060 回答
16

为当前目录中的文件启动一个简单的 Web 服务器:

python -m SimpleHTTPServer

用于共享文件。

于 2011-09-16T15:40:14.590 回答
14

一个“进度条”,如下所示:

|#############################---------------------|
59 percent done

代码:

class ProgressBar():
    def __init__(self, width=50):
        self.pointer = 0
        self.width = width

    def __call__(self,x):
         # x in percent
         self.pointer = int(self.width*(x/100.0))
         return "|" + "#"*self.pointer + "-"*(self.width-self.pointer)+\
                "|\n %d percent done" % int(x) 

测试功能(windows系统,将“clear”改为“CLS”):

if __name__ == '__main__':
    import time, os
    pb = ProgressBar()
    for i in range(101):
        os.system('clear')
        print pb(i)
        time.sleep(0.1)
于 2010-12-13T16:27:04.580 回答
11

展平列表列表,例如

[['a', 'b'], ['c'], ['d', 'e', 'f']]

进入

['a', 'b', 'c', 'd', 'e', 'f']

采用

[inner
    for outer in the_list
        for inner in outer]
于 2009-03-29T01:47:41.923 回答
10

嵌套列表和字典的巨大加速:

deepcopy = lambda x: cPickle.loads(cPickle.dumps(x))
于 2009-03-28T21:36:56.050 回答
8

假设您有一个项目列表,并且您想要一个以这些项目作为键的字典。使用 fromkeys:

>>> items = ['a', 'b', 'c', 'd']
>>> idict = dict().fromkeys(items, 0)
>>> idict
{'a': 0, 'c': 0, 'b': 0, 'd': 0}
>>>

fromkeys 的第二个参数是要授予所有新创建的键的值。

于 2009-03-28T08:36:40.793 回答
7

要确定行是否为空(即大小为 0 或仅包含空格),请在条件中使用字符串方法 strip,如下所示:

if not line.strip():    # if line is empty
    continue            # skip it
于 2009-03-28T08:35:49.173 回答
5

我喜欢这个将所有内容压缩到一个目录中。热键它用于 instabackups!

import zipfile

z = zipfile.ZipFile('my-archive.zip', 'w', zipfile.ZIP_DEFLATED)
startdir = "/home/johnf"
for dirpath, dirnames, filenames in os.walk(startdir):
  for filename in filenames:
    z.write(os.path.join(dirpath, filename))
z.close()
于 2009-03-28T01:13:34.323 回答
5

对于需要当前的列表推导,下一步:

[fun(curr,next) 
 for curr,next 
 in zip(list,list[1:].append(None)) 
 if condition(curr,next)] 

对于循环列表zip(list,list[1:].append(list[0]))

对于以前,当前:zip([None].extend(list[:-1]),list)循环:zip([list[-1]].extend(list[:-1]),list)

于 2009-03-28T21:32:02.100 回答
4

硬链接当前目录中的相同文件(在 unix 上,这意味着它们共享物理存储,这意味着空间要少得多):

import os
import hashlib

dupes = {}

for path, dirs, files in os.walk(os.getcwd()):
    for file in files:
        filename = os.path.join(path, file)
        hash = hashlib.sha1(open(filename).read()).hexdigest()
        if hash in dupes:
            print 'linking "%s" -> "%s"' % (dupes[hash], filename)
            os.rename(filename, filename + '.bak')
            try:
                os.link(dupes[hash], filename)
                os.unlink(filename + '.bak')
            except:
                os.rename(filename + '.bak', filename)
            finally:
        else:
            dupes[hash] = filename
于 2009-03-28T01:10:48.713 回答
3

以下是我认为值得了解但可能对日常没有用的一些内容。他们中的大多数是一个班轮。

从列表中删除重复项

L = list(set(L))

从字符串中获取整数(空格分隔)

ints = [int(x) for x in S.split()]

寻找阶乘

fac=lambda(n):reduce(int.__mul__,range(1,n+1),1)

求最大公约数

>>> def gcd(a,b):
...     while(b):a,b=b,a%b
...     return a
于 2011-04-18T19:15:51.243 回答
2

模拟 switch 语句。例如 switch(x) {..}:

def a():
  print "a"

def b():
  print "b"

def default():
   print "default"

apply({1:a, 2:b}.get(x, default))
于 2011-04-17T14:38:13.270 回答
2
  • 像上面的另一个人一样,我说“哇!!” 当我发现enumerate()

  • 当我发现repr()让我有可能准确地看到我想用正则表达式分析的字符串的内容时,我对 Python 赞不绝口

  • 我很满意地发现'\n'.join(...)print '\n'.join(list_of_strings)的显示速度比for ch in list_of_strings: print ch

  • 带参数的splitlines(1)保留换行符

这四个“技巧”组合在一个片段中非常有用,可以快速显示网页的代码源,逐行显示,每一行都被编号,所有特殊字符(如 '\t' 或换行符都不会被解释,以及换行符)展示:

import urllib
from time import clock,sleep

sock = urllib.urlopen('http://docs.python.org/')
ch = sock.read()
sock.close()


te = clock()
for i,line in enumerate(ch.splitlines(1)):
    print str(i) + ' ' + repr(line)
t1 = clock() - te


print "\n\nIn 3 seconds, I will print the same content, using '\\n'.join(....)\n" 

sleep(3)

te = clock()
# here's the point of interest:
print '\n'.join(str(i) + ' ' + repr(line)
                for i,line in enumerate(ch.splitlines(1)) )
t2 = clock() - te

print '\n'
print 'first  display took',t1,'seconds'
print 'second display took',t2,'seconds'

用我不是很快的电脑,我得到了:

first  display took 4.94626048841 seconds
second display took 0.109297410704 seconds
于 2011-04-17T16:41:15.717 回答
2
import tempfile
import cPickle

class DiskFifo:
    """A disk based FIFO which can be iterated, appended and extended in an interleaved way"""
    def __init__(self):
        self.fd = tempfile.TemporaryFile()
        self.wpos = 0
        self.rpos = 0
        self.pickler = cPickle.Pickler(self.fd)
        self.unpickler = cPickle.Unpickler(self.fd)
        self.size = 0

    def __len__(self):
        return self.size

    def extend(self, sequence):
        map(self.append, sequence)

    def append(self, x):
        self.fd.seek(self.wpos)
        self.pickler.clear_memo()
        self.pickler.dump(x)
        self.wpos = self.fd.tell()
        self.size = self.size + 1

    def next(self):
        try:
            self.fd.seek(self.rpos)
            x = self.unpickler.load()
            self.rpos = self.fd.tell()
            return x

        except EOFError:
            raise StopIteration

    def __iter__(self):
        self.rpos = 0
        return self
于 2011-08-18T00:28:38.123 回答
1

对于 Python 2.4+ 或更早版本:

for x,y in someIterator:
  listDict.setdefault(x,[]).append(y)

在 Python 2.5+ 中有使用defaultdict的替代方法。

于 2009-03-29T10:46:58.430 回答
1

一个自定义列表,当乘以其他列表时返回一个笛卡尔积......好在笛卡尔积是可索引的,不像 itertools.product 那样(但被乘数必须是序列,而不是迭代器)。

import operator

class mylist(list):
    def __getitem__(self, args):
        if type(args) is tuple:
            return [list.__getitem__(self, i) for i in args]
        else:
            return list.__getitem__(self, args)
    def __mul__(self, args):
        seqattrs = ("__getitem__", "__iter__", "__len__")
        if all(hasattr(args, i) for i in seqattrs):
            return cartesian_product(self, args)
        else:
            return list.__mul__(self, args)
    def __imul__(self, args):
        return __mul__(self, args)
    def __rmul__(self, args):
        return __mul__(args, self)
    def __pow__(self, n):
        return cartesian_product(*((self,)*n))
    def __rpow__(self, n):
        return cartesian_product(*((self,)*n))

class cartesian_product:
    def __init__(self, *args):
        self.elements = args
    def __len__(self):
        return reduce(operator.mul, map(len, self.elements))
    def __getitem__(self, n):
        return [e[i] for e, i  in zip(self.elements,self.get_indices(n))]
    def get_indices(self, n):
        sizes = map(len, self.elements)
        tmp = [0]*len(sizes)
        i = -1
        for w in reversed(sizes):
            tmp[i] = n % w
            n /= w
            i -= 1
        return tmp
    def __add__(self, arg):
        return mylist(map(None, self)+mylist(map(None, arg)))
    def __imul__(self, args):
        return mylist(self)*mylist(args)
    def __rmul__(self, args):
        return mylist(args)*mylist(self)
    def __mul__(self, args):
        if isinstance(args, cartesian_product):
            return cartesian_product(*(self.elements+args.elements))
        else:
            return cartesian_product(*(self.elements+(args,)))
    def __iter__(self):
        for i in xrange(len(self)):
            yield self[i]
    def __str__(self):
        return "[" + ",".join(str(i) for i in self) +"]"
    def __repr__(self):
        return "*".join(map(repr, self.elements))
于 2009-07-12T13:23:42.953 回答
1

实际上我只是创建了这个,但我认为它将是一个非常有用的调试工具。

def dirValues(instance, all=False):
    retVal = {}
    for prop in dir(instance):
        if not all and prop[1] == "_":
            continue
        retVal[prop] = getattr(instance, prop)
    return retVal

我通常在 pdb 上下文中使用 dir() ,但我认为这会更有用:

(pdb) from pprint import pprint as pp
(pdb) from myUtils import dirValues
(pdb) pp(dirValues(someInstance))
于 2011-11-20T19:39:03.640 回答
1

通过 x 元素块迭代任何大小(包括未知大小)的任何可迭代对象(列表、集合、文件、流、字符串等):

from itertools import chain, islice

def chunks(iterable, size, format=iter):
    it = iter(iterable)
    while True:
        yield format(chain((it.next(),), islice(it, size - 1)))

>>> l = ["a", "b", "c", "d", "e", "f", "g"]
>>> for chunk in chunks(l, 3, tuple):
...         print chunk
...     
("a", "b", "c")
("d", "e", "f")
("g",)
于 2012-08-07T08:30:43.127 回答
0

调试时,有时您希望使用基本编辑器查看字符串。用记事本显示字符串:

import os, tempfile, subprocess

def get_rand_filename(dir_=os.getcwd()):
    "Function returns a non-existent random filename."
    return tempfile.mkstemp('.tmp', '', dir_)[1]

def open_with_notepad(s):
    "Function gets a string and shows it on notepad"
    with open(get_rand_filename(), 'w') as f:
        f.write(s)
        subprocess.Popen(['notepad', f.name])
于 2012-10-07T15:13:47.557 回答