使用Python的本机sum函数和NumPy 之间在性能和行为上有什么不同numpy.sum?sum适用于NumPy的数组,numpy.sum可以在Python列表上运行,它们都返回相同的有效结果(没有测试边缘情况,如溢出)但不同类型.
>>> import numpy as np
>>> np_a = np.array(range(5))
>>> np_a
array([0, 1, 2, 3, 4])
>>> type(np_a)
<class 'numpy.ndarray')
>>> py_a = list(range(5))
>>> py_a
[0, 1, 2, 3, 4]
>>> type(py_a)
<class 'list'>
# The numerical answer (10) is the same for the following sums:
>>> type(np.sum(np_a))
<class 'numpy.int32'>
>>> type(sum(np_a))
<class 'numpy.int32'>
>>> type(np.sum(py_a))
<class 'numpy.int32'>
>>> type(sum(py_a))
<class 'int'>
Run Code Online (Sandbox Code Playgroud)
编辑:我认为我的实际问题是numpy.sum在Python整数列表上使用比使用Python自己更快sum吗?
另外,使用Python整数与标量有 …
在做一些生物信息学工作时,我一直在思考将对象实例存储在Numpy数组而不是Python列表中的后果,但在我所做的所有测试中,性能在每个实例中都更糟.我正在使用CPython.有谁知道原因?
特别:
numpy.ndarray(dtype=object)与常规Python列表有什么性能影响?我执行的初始测试表明,访问Numpy数组元素比通过Python列表迭代更慢,尤其是在使用对象方法时.[ X() for i in range(n) ]代替numpy.empty(size=n, dtype=object)?__slots__,如果这有任何影响.我在这里看到了这个问题,但是给出的答案在我的案例中没有用,并且被标记为重复.
python -ustdin在Python 3中不起作用.sys.stdin = sys.stdin.detach()抛出一个ValueError: underlying buffer has been detached.stdin输入和其他文件用作流.FileInput(openhook=hook_nobuf)并open(buffering=0)在钩子中使用.我挖了源代码(/usr/lib/python3.2/fileinput.py)并看到它readlines(bufsize)在内部用于加载缓冲区.没有贝壳或其他管道的恶作剧.
CONFIGS/routes.rb中
Shutters和Paints是子资源Jobs.
resources :jobs do
resources :shutters
resources :paints
end
Run Code Online (Sandbox Code Playgroud)
app/models/job.rb
A Job包含很多Shutters很多Paints.
class Job < ActiveRecord::Base
has_many :shutters, dependent: :delete_all
has_many :paints, dependent: :delete_all
accepts_nested_attributes_for :shutters, allow_destroy: true, :reject_if => lambda { |a| a[:no].blank? }
accepts_nested_attributes_for :paints, allow_destroy: true, :reject_if => lambda { |a| a[:name].blank? }`
Run Code Online (Sandbox Code Playgroud)
app/models/shutter.rb
A Shutter包含属于一个Job和一个Paint.
class Shutter < ActiveRecord::Base
belongs_to :job
belongs_to :paint
Run Code Online (Sandbox Code Playgroud)
app/models/paint.rb
A Paint属于一个,Job但Shutters …
我正在尝试重新引发异常,以便为用户提供有关实际错误的更好信息.Python 3.3包括PEP 409.它添加了raise NewException from None语法来抑制原始异常的上下文.
但是,我的目标是Python 3.2.Python脚本将解析,但在运行时如果遇到from None它将产生的语法 TypeError: exception causes must derive from BaseException.例如:
try:
regex_c = re.compile('^{}$'.format(regex))
except re.error as e:
e_msg = 'Regular expression error in "{}"'.format(regex)
e_reraise = Exception(e_msg)
# Makes use of the new Python 3.3 exception syntax [from None]
# to suppress the context of the original exception
# Causes an additional TypeError exception in Python 3.2
raise e_reraise from None
Run Code Online (Sandbox Code Playgroud)
封装raise e_reraise from None …
python exception forward-compatibility python-3.2 python-3.3
python ×4
numpy ×2
performance ×2
python-3.x ×2
buffering ×1
cpython ×1
exception ×1
file-io ×1
python-3.2 ×1
python-3.3 ×1
readline ×1
readlines ×1
ruby ×1
ruby-2.1 ×1
sqlite ×1