小编thi*_*dam的帖子

为什么如果True比1慢?

为什么if Trueif 1Python 慢?不if True应该比快if 1

我试图学习这个timeit模块.从基础知识开始,我尝试了以下方法:

>>> def test1():
...     if True:
...         return 1
...     else:
...         return 0

>>> print timeit("test1()", setup = "from __main__ import test1")
0.193144083023


>>> def test2():
...     if 1:
...         return 1
...     else:
...         return 0

>>> print timeit("test2()", setup = "from __main__ import test2")
0.162086009979


>>> def test3():
...     if True:
...             return True
...     else:
...             return False

>>> …
Run Code Online (Sandbox Code Playgroud)

python performance if-statement boolean timeit

29
推荐指数
2
解决办法
7052
查看次数

Python的逗号运算符在赋值期间如何工作?

我正在阅读Python文档中的赋值语句(http://docs.python.org/reference/simple_stmts.html#assignment-statements).

因此引用:

如果目标是括在括号中或方括号中的目标列表:对象必须是与目标列表中的目标具有相同数量的项目的可迭代对象,并且其项目从左到右分配给相应的目标.

读完之后,我想写一个这样的样本:

a = 5
b = 4
a, b = a + b, a
print a, b
Run Code Online (Sandbox Code Playgroud)

我的假设是a和b都应该具有9的值.

但是,我被证明是错的.'a'的值为9,'b'的值为5.

有人可以帮助我更好地理解这一点吗?为什么要分配旧值'a'而不是新值?根据文档,首先会分配一个值?我错过了什么吗?

python tuples comma variable-assignment

27
推荐指数
3
解决办法
2万
查看次数

__init__是一种类方法吗?

我正在研究Python的超级方法和多重继承.我读到的东西就像当我们使用super来调用一个在所有基类中都有实现的基本方法时,即使有各种参数,也只会调用一个类的方法.例如,

class Base1(object):
    def __init__(self, a):
        print "In Base 1"

class Base2(object):
    def __init__(self):
        print "In Base 2"

class Child(Base1, Base2):
    def __init__(self):
        super(Child, self).__init__('Intended for base 1')
        super(Child, self).__init__()# Intended for base 2
Run Code Online (Sandbox Code Playgroud)

这产生TyepError了第super一种方法.super会调用它首先识别的方法实现,TypeError而不是检查其他类.但是,当我们执行以下操作时,这将更加清晰并且正常工作:

class Child(Base1, Base2):
    def __init__(self):
        Base1.__init__(self, 'Intended for base 1')
        Base2.__init__(self) # Intended for base 2
Run Code Online (Sandbox Code Playgroud)

这导致两个问题:

  1. __init__方法的静态方法或类方法?
  2. 为什么要使用super,它隐式选择自己的方法,而不是像后一个例子那样显式调用方法?它看起来比使用super更清洁.那么使用super第二种方式的优点是什么(除了使用方法调用编写基类名称)

python inheritance multiple-inheritance python-2.7

8
推荐指数
2
解决办法
2113
查看次数

多核处理器中的线程

我用C编写了一个程序,我只想在另一个处理器核心中运行一个函数.

有谁知道如何在不同的核心上分配线程?在一个核心上运行的单个运行线程是否有时可以使用另一个核心,具体取决于核心的繁忙程度?

c linux multithreading

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

从另一个字典python中删除一个字典

如果 A 和 B 是两个字典,使用 python,有没有办法从字典 A 中删除字典 B 中的元素?

例如,

parent_dict = {"a" : "aaa", "b" : "bbb", "c" : "ccc", "d" : "ddd", "e": "eee"}
derived_dict = {"a" : "aaa", "d" : "ddd", "e" : "eee"}
Run Code Online (Sandbox Code Playgroud)

现在我需要编写一个函数 dict_reduce(dictA, dictB) 从 dictA 中删除 dictB 的所有元素。

(即) dict_reduce(parent_dict,derived_dict) 应该给 {"b" : "bbb", "c" : "ccc"}

我使用 for 循环的解决方法是:

def dict_reduce(parent_dict, child_dict):
    for key in child_dict.keys():
        del parent_dict[key]
    return parent_dict

reduced_dict = dict_reduce(parent_dict, child_dict)
Run Code Online (Sandbox Code Playgroud)

笔记:

  1. 如果解决方案是单行或不需要 for 循环的东西,那就太好了。
  2. 另外我们不需要在删除之前检查父字典是否有键,因为子字典是从父字典派生的。所以不用考虑keyError。
  3. 父字典是一个静态字典,不应受该方法的影响。相反,返回的字典应该存储在另一个简化的字典中。
  4. 无需检查 …

python dictionary

2
推荐指数
2
解决办法
7669
查看次数