小编thk*_*ang的帖子

对于大型数组而言比列表慢得多?

检查我的以下代码; 它是在python中实现的sigma_2函数(使用原始筛选)的一部分,它是除数函数之一http://mathworld.wolfram.com/DivisorFunction.html

from time import time
from itertools import count
import numpy

def sig2(N, nump=False):
    init = time()


    #initialize array with value=1 since every positive integer is divisible by 1
    if nump:
        print 'using numpy'
        nums = numpy.ones((N,), dtype=numpy.int64)
    else:        
        nums = [1 for i in xrange(1, N)]

    #for each number n < N, add n*n to n's multiples
    for n in xrange(2, N):
        nn = n*n
        for i in count(1):
            if n*i >= N: break
            nums[n*i-1] += nn …
Run Code Online (Sandbox Code Playgroud)

python numpy

0
推荐指数
1
解决办法
2426
查看次数

python __getattribute__覆盖和@property装饰器

我不得不写一些覆盖的类__getattribute__.基本上我的类是一个容器,它将每个用户添加的属性保存到self._meta字典中.

class Container(object):
    def __init__(self, **kwargs):
        super(Container, self).__setattr__('_meta', OrderedDict())
        #self._meta = OrderedDict()
        super(Container, self).__setattr__('_hasattr', lambda key : key in self._meta)

        for attr, value in kwargs.iteritems():
            self._meta[attr] = value

    def __getattribute__(self, key):
        try:
            return super(Container, self).__getattribute__(key)
        except:
            if key in self._meta : return self._meta[key]
            else:
                raise AttributeError, key

    def __setattr__(self, key, value):
        self._meta[key] = value
#usage:
>>> a = Container()
>>> a
<__main__.Container object at 0x0000000002B2DA58>
>>> a.abc = 1 #set an attribute
>>> a._meta
OrderedDict([('abc', 1)]) …
Run Code Online (Sandbox Code Playgroud)

python attributes class

0
推荐指数
1
解决办法
2751
查看次数

为什么我的字符串格式不起作用?

我正在使用python动态创建一个jQuery函数:

jQuery = ("$('%(other_id)').click(function() { "
              "    if ($(this).is(':checked')) { "
              "        $('%(text_id)').show()     "
              "    } "
              "    else {"
              "       $('%(text_id)').hide()"
              "    }"
              " });")
Run Code Online (Sandbox Code Playgroud)

我要插入可变因素成other_idtext_id.我看到该$符号用于字符串模板(不知道它做了什么)所以我用double- $s($$)来逃避它们

jQuery = ("$$('%(other_id)').click(function() { "
              "    if ($$(this).is(':checked')) { "
              "        $$('%(text_id)').show()     "
              "    } "
              "    else {"
              "       $$('%(text_id)').hide()"
              "    }"
              " });")
Run Code Online (Sandbox Code Playgroud)

但是我还是不能格式化这个:

>>> choice_id = 'foo'
>>> text_choice_id = 'bar'
>>> jQuery = ("$$('%(other_id)').click(function() { "
                  "    if ($$(this).is(':checked')) …
Run Code Online (Sandbox Code Playgroud)

python string format

0
推荐指数
1
解决办法
354
查看次数

使用if语句列表理解(或单行)

在python中,对于iterables,可以构造许多单行迭代命令.

对于某些此类迭代,需要if语句.

有时if语句和for语句的顺序很重要.假设我想找到0到10之间的奇数之和:

>>> sum(i if not i%2==0 for i in range(10))
SyntaxError: invalid syntax
>>> sum(i for i in range(10) if not i%2==0)
25
Run Code Online (Sandbox Code Playgroud)

这些单行本身不是很理解,但我真的不明白为什么if语句有来的for语句.使用前一个不是更合适的常识i if not i%2==0 for i in range(10)吗?

python if-statement list-comprehension

-3
推荐指数
1
解决办法
322
查看次数