为了与"只有一种明显的方法"相符,你如何在Numpy中获得向量(1D数组)的大小?
def mag(x):
return math.sqrt(sum(i**2 for i in x))
Run Code Online (Sandbox Code Playgroud)
上面的作品,但我不敢相信我必须自己指定这样一个琐碎的核心功能.
感谢来自这里的人们的帮助,我能够获得塔斯马尼亚骆驼拼图工作的代码.然而,它非常慢(我想.我不确定,因为这是我在Python中的第一个程序).在代码底部运行的示例需要很长时间才能在我的机器中解决:
dumrat@dumrat:~/programming/python$ time python camels.py
[['F', 'F', 'F', 'G', 'B', 'B', 'B'], ['F', 'F', 'G', 'F', 'B', 'B', 'B'],
['F', 'F', 'B', 'F', 'G', 'B', 'B'], ['F', 'F', 'B', 'F', 'B', 'G', 'B'],
['F', 'F', 'B', 'G', 'B', 'F', 'B'], ['F', 'G', 'B', 'F', 'B', 'F', 'B'],
['G', 'F', 'B', 'F', 'B', 'F', 'B'], ['B', 'F', 'G', 'F', 'B', 'F', 'B'],
['B', 'F', 'B', 'F', 'G', 'F', 'B'], ['B', 'F', 'B', 'F', 'B', 'F', 'G'],
['B', 'F', 'B', 'F', 'B', …Run Code Online (Sandbox Code Playgroud)