我正在尝试加载我之前保存的稀疏数组.保存稀疏数组很容易.试图阅读它是一种痛苦.scipy.load在我的稀疏数组周围返回一个0d数组.
import scipy as sp
A = sp.load("my_array"); A
array(<325729x325729 sparse matrix of type '<type 'numpy.int8'>'
with 1497134 stored elements in Compressed Sparse Row format>, dtype=object)
Run Code Online (Sandbox Code Playgroud)
为了获得稀疏矩阵,我必须展平0d数组,或者使用sp.asarray(A).这似乎是一种非常难以做到的事情.Scipy是否足够聪明才能理解它已经加载了一个稀疏数组?有没有更好的方法来加载稀疏数组?
我无法理解这一点......
无法运行此代码,我不知道为什么它是语法错误.
try:
newT.read()
#existingArtist = newT['Exif.Image.Artist'].value
#existingKeywords = newT['Xmp.dc.subject'].value
except KeyError:
print "KeyError"
else:
#Program will NOT remove existing values
newT.read()
if existingArtist != "" :
newT['Exif.Image.Artist'] = artistString
print existingKeywords
keywords = os.path.normpath(relativePath).split(os.sep)
print keywords
newT['Xmp.dc.subject'] = existingKeywords + keywords
newT.write()
except:
print "Cannot write tags to ",filePath
Run Code Online (Sandbox Code Playgroud)
最后一个"except:"发生语法错误.再次......我不知道为什么python会抛出一个语法错误(在这个问题上花了大约3小时).
我有一个我写的装饰器,它将为给定的函数计时.它似乎适用于任何函数,除了递归函数.
装饰:
def tictoc(repeats=3, loops=1):
def t(func):
from functools import partial, wraps
import timeit
@wraps(func)
def timer(*args, **kargs):
elapsed = timeit.repeat(partial(func, *args, **kargs), repeat = repeats, number=loops)
mine = min(elapsed)
print "%s finished in %.5fs (%d loops, %d times) with %.5fs per loop" % (func.__name__, mine, loops, repeats, mine/loops)
return timer
return t
Run Code Online (Sandbox Code Playgroud)
递归函数是基本的Fibonacci算法.
@tictoc()
def fib(i):
return ( 0 if i == 0 else
1 if i == 1 else
fib(i-1) + fib(i-2) )
fib(15)
Run Code Online (Sandbox Code Playgroud)
程序失败,出现以下错误
fib finished in …Run Code Online (Sandbox Code Playgroud)