小编mgi*_*son的帖子

Cython安装没有找到Python.h文件?

我想在我的ubuntu 12.04上安装cython,然后进入终端

sudo easy_install cython
Run Code Online (Sandbox Code Playgroud)

作为回应,我收到以下错误:

Searching for cython  
Reading http://pypi.python.org/simple/cython/  
Reading http://www.cython.org  
Reading http://cython.org  
Best match: Cython 0.16  
Downloading http://www.cython.org/release/Cython-0.16.zip  
Processing Cython-0.16.zip  
Running Cython-0.16/setup.py -q bdist_egg --dist-dir /tmp/easy_install-VzJ0lH/Cython-0.16/egg-dist-tmp-BMJs3p  
Compiling module Cython.Plex.Scanners ...  
Compiling module Cython.Plex.Actions ...  
Compiling module Cython.Compiler.Lexicon ...  
Compiling module Cython.Compiler.Scanning ...  
Compiling module Cython.Compiler.Parsing ...  
Compiling module Cython.Compiler.Visitor ...  
Compiling module Cython.Compiler.FlowControl ...  
Compiling module Cython.Compiler.Code ...  
Compiling module Cython.Runtime.refnanny ...  
warning: no files found matching '*.pyx' under directory 'Cython/Debugger/Tests'  
warning: no files found matching '*.pxd' …
Run Code Online (Sandbox Code Playgroud)

python cython

8
推荐指数
1
解决办法
7512
查看次数

fortran中的二元搜索效率与线性搜索效率

这个问题是关于线性搜索的效率与连续存储中预排序数组的二进制搜索的效率...

我有一个用fortran编写的应用程序(77!).我的部分代码的一个常见操作是在数组中找到索引gx(i) <= xin < gx(i+1).我目前已经实现了这个binary search- 对于声明标签而言goto- 并且- 我已经评论了使用fortran 90的等效声明...

        i=1
        ih=nx/2
201     continue  !do while (.true.)
           if((xin.le.gx(i)).and.(xin.gt.gx(i+1)))then  !found what we want
              ilow=i+1; ihigh=i
              s1=(gx(ihigh)-xin)/(gx(ihigh)-gx(ilow))
              s2=1.0-s1
              return
           endif
           if(i.ge.ih)then
              goto 202 !exit
           endif
           if(xin.le.(gx(ih))then !xin is in second half of array
              i=ih
              ih=nx-(nx-ih)/2
           else !xin is in first half of array
              i=i+1
              ih=i+(ih-i)/2
           endif
        goto 201  !enddo
Run Code Online (Sandbox Code Playgroud)

然而,今天,我正在维基百科上阅读二进制搜索,我发现了这个:

Binary search can interact poorly with the memory hierarchy 
(i.e. caching), because of its random-access …
Run Code Online (Sandbox Code Playgroud)

performance fortran binary-search fortran77 linear-search

8
推荐指数
1
解决办法
3312
查看次数

提高NumPy阵列上的操作性能

我正在使用numpy.delete从while循环内的数组中删除元素.仅当数组不为空时,此while循环才有效.此代码工作正常,但当数组超过1e6元素时,速度会大大降低.这是一个例子:

while(array.shape[0] > 0):
     ix = where((array >= x) & (array <= y))[0]
     array = delete(array,ix,None)
Run Code Online (Sandbox Code Playgroud)

我试图使这个代码有效,但我找不到一个加速while循环的好方法.我认为,这里的瓶颈是必须包含某种副本的删除.我已经尝试使用蒙面数组以避免复制,但我不擅长python和掩码数组不是那么容易搜索.有没有一种好的,快速的方法来使用删除或替换它,以便7e6元素可以通过上面的循环处理而不需要24小时?

谢谢

python numpy

8
推荐指数
1
解决办法
3598
查看次数

从字典列表中删除键值对

我有一个词典列表,例如:

[{'mykey1':'myvalue1', 'mykey2':'myvalue2'}, {'mykey1':'myvalue1a', 'mykey2':'myvalue2a'}]
Run Code Online (Sandbox Code Playgroud)

我需要从密钥等于mykey1的所有字典中删除所有键值对.我可以通过循环并使用del语句来做到这一点,但我想知道如何使用列表推导或lambdas创建一个新列表,它将删除密钥为mykey1的所有键值对.

非常感谢

python list-comprehension

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

python re.X vs自动线延续

Python提供了一个标志(re.Xre.VERBOSE)来允许正则表达式的注释:

a = re.compile(r"""\d +  # the integral part
                   \.    # the decimal point
                   \d *  # some fractional digits""", re.X)
Run Code Online (Sandbox Code Playgroud)

但是,使用自动字符串连接,您可以实现基本相同的事情:

a = re.compile(r'\d+' # integral part
               r'\.'  # decimal point
               r'\d*' # optional fractional digits
              )
Run Code Online (Sandbox Code Playgroud)

我不认为我真的看到后一种形式使用,但(恕我直言)它使得更容易阅读正则表达式(我不需要试图找出哪些空格已被转义,以及是什么空白正在使用忽略...等等.)我的评论由我的文本编辑器格式化为评论.是否有理由更喜欢前者而不是后者或签证?或者这真的是番茄 - 番茄问题?

python regex comments annotations

8
推荐指数
1
解决办法
874
查看次数

英特尔Fortran - 检查界限不提供行号

ifort在mpi环境中使用intel fortran编译器().事实证明,当我使用标志编译时,我的代码有一个缓冲区溢出-g -O2 -check bounds.运行一段时间后,我收到此消息:

forrtl: severe (408): fort: (2): Subscript #1 of the array XX has value 4 which is greater than the upper bound of 3

Image            PC                Routine            Line        Source             
program.exe      00000000006E757E  Unknown               Unknown  Unknown
program.exe      00000000006E6016  Unknown               Unknown  Unknown
program.exe      00000000006905B2  Unknown               Unknown  Unknown
program.exe      0000000000642E6B  Unknown               Unknown  Unknown
program.exe      0000000000643381  Unknown               Unknown  Unknown
program.exe      00000000005F33FB  Unknown               Unknown  Unknown
program.exe      00000000004139E7  Unknown               Unknown  Unknown
program.exe      000000000040A6B4  Unknown               Unknown  Unknown
program.exe      0000000000409D2C  Unknown …
Run Code Online (Sandbox Code Playgroud)

fortran command-line-arguments intel-fortran

8
推荐指数
1
解决办法
4804
查看次数

查找具有最大乘积的一组整数的子集

设A是一组非空整数.编写一个函数find,输出具有最大乘积的A的非空子集.例如,find([ - 1,-2,-3,0,2])= 12 =( - 2)*( - 3)*2

这就是我的想法:将列表分成正整数列表和负整数列表:

  1. 如果我们有一个偶数个负整数,那么将两个列表中的所有内容相乘,我们就得到了答案.
  2. 如果我们有一个奇数个负整数,找到最大值并将其从列表中删除.然后将两个列表中的所有内容相乘.
  3. 如果列表只有一个元素,则返回此元素.

这是我在Python中的代码:

def find(xs):
    neg_int = []
    pos_int = []
    if len(xs) == 1:
        return str(xs[0])
    for i in xs:
        if i < 0:
            neg_int.append(i)
        elif i > 0:
            pos_int.append(i)
    if len(neg_int) == 1 and len(pos_int) == 0 and 0 in xs:
        return str(0)
    if len(neg_int) == len(pos_int) == 0:
        return str(0)
    max = 1
    if len(pos_int) > 0:
        for x in pos_int:
            max=x*max
    if …
Run Code Online (Sandbox Code Playgroud)

python algorithm

8
推荐指数
1
解决办法
1037
查看次数

为什么我会收到NameError?

我有以下代码:

from crypt import crypt
from itertools import product
from string import ascii_letters, digits

def decrypt(all_hashes, salt, charset=ascii_letters + digits + "-"):
     products = (product(charset, repeat=r) for r in range(8))
     chain = itertools.chain.from_iterable(products)
     for candidate in chain:
         hash = crypt(candidate, salt)
         if hash in all_hashes:
              yield candidate, hash
              all_hashes.remove(hash)
              if not all_hashes:
                 return

all_hashes = ['aaRrt6qwqR7xk', 'aaacT.VSMxhms' , 'aaWIa93yJI9kU',
'aakf8kFpfzD5E', 'aaMOPiDnXYTPE', 'aaz71s8a0SSbU', 'aa6SXFxZJrI7E'
'aa9hi/efJu5P.', 'aaBWpr07X4LDE', 'aaqwyFUsGMNrQ', 'aa.lUgfbPGANY'
'aaHgyDUxJGPl6', 'aaTuBoxlxtjeg', 'aaluQSsvEIrDs', 'aajuaeRAx9C9g'
'aat0FraNnWA4g', 'aaya6nAGIGcYo', 'aaya6nAGIGcYo', 'aawmOHEectP/g'
'aazpGZ/jXGDhw', 'aadc1hd1Uxlz.', 'aabx55R4tiWwQ', 'aaOhLry1KgN3.'
'aaGO0MNkEn0JA', …
Run Code Online (Sandbox Code Playgroud)

python

7
推荐指数
1
解决办法
1万
查看次数

有没有办法在没有try/except的情况下对异常提升到程序顶部?

我可以捕获并转储一个异常(和相应的堆栈跟踪),这会导致程序崩溃而不执行以下操作:

try:
   # whole program
except Execption as e:
   dump(e)
   raise
Run Code Online (Sandbox Code Playgroud)

有时外部库崩溃了,我想对Python死亡做出反应并记录它的原因.我不想阻止Exception崩溃程序,我只想要调试信息.

就像是:

signals.register('dying', callback)

def callback(context):
    # dumping the exception and
    # stack trace from here
Run Code Online (Sandbox Code Playgroud)

这甚至可能吗?

python exception

7
推荐指数
1
解决办法
147
查看次数

创建多维零点Python

我需要制作一个零的多维数组.

对于两个(D = 2)或三个(D = 3)尺寸,这很容易,我会使用:

a = numpy.zeros(shape=(n,n)) 
Run Code Online (Sandbox Code Playgroud)

要么

a = numpy.zeros(shape=(n,n,n))
Run Code Online (Sandbox Code Playgroud)

如何为更高的D,制作长度为n的数组?

python arrays numpy multidimensional-array

7
推荐指数
1
解决办法
2万
查看次数