我想在Fortran 中做这样的事情:
program where
real :: a(6) = (/ 4, 5, 6, 7, 8, 9 /)
print *, a(a>7)
end program
Run Code Online (Sandbox Code Playgroud)
在Python 中,我通常会像这样使用NumPy 执行此操作:
program where
real :: a(6) = (/ 4, 5, 6, 7, 8, 9 /)
print *, a(a>7)
end program
Run Code Online (Sandbox Code Playgroud)
我玩过,但到目前为止没有任何效果,但我猜这很简单。
有没有比使用numpy.asarray()以list?形式从输出生成数组更有效的方法?
这似乎是复制内存中的所有内容,这对于非常大的数组来说似乎并不高效.
(更新)示例:
import numpy as np
a1 = np.array([1,2,3,4,5,6,7,8,9,10]) # pretend this has thousands of elements
a2 = np.array([3,7,8])
results = np.asarray([np.amax(np.where(a1 > element)) for element in a2])
Run Code Online (Sandbox Code Playgroud) 是否可以使用 gdal 的 WriteArray 逐行写入数据,而不是创建并向其提供整个数组?
MemoryError我在创建大小为 (50539,98357) 的 numpy 数组时遇到了问题。我想我可以通过使用 PyTables 来解决这个问题,但我不想让事情复杂化
>>> import numpy
>>> cols = 50539
>>> rows = 98357
>>> a1 = np.zeros((cols,rows))
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'np' is not defined
>>> import numpy as np
>>> a1 = np.zeros((cols,rows))
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
MemoryError
Run Code Online (Sandbox Code Playgroud)
更新: 我最终使用 Abudis 的解决方案与稀疏矩阵相结合,在其中保存了点,将每一行拉出为“密集”或标准矩阵。欢迎评论。
dataset = driver.Create(filename, cols, rows, number_of_bands, band_type)
band …Run Code Online (Sandbox Code Playgroud) 我决定在我的python代码中从制表符切换到空格.以前,如果我想在vim中缩进/取消一段代码,我会使用>>或<<命令.
我正在使用python的vimrc设置添加到我的~/.vimrc:
source ~/.vimrc-python
Run Code Online (Sandbox Code Playgroud)
目前,似乎它将新选项卡设置为8个空格宽,当我阻止缩进/非缩进时,它将所有内容移动四个空格.
我怎么能得到一切以使它一致?
对于文本写作中vim,当我需要编辑一个线/添加一些新的文字,每行字的数量得到所有走出低谷.
有没有办法快速重新调整/换行以均匀分散VIM中每行的字数?
80字符自动换行的示例:
在编辑之前
This is something that I have writting with automatic line-wrapping before I
have edited the file. I'll write some more just for demonstration purposes for
this whole line wrapping problem of mine.
Run Code Online (Sandbox Code Playgroud)
编辑后
This is something that I have writting with automatic line-wrapping after I
have edited the file. I'm demonstrating
this whole line wrapping problem of mine.
Run Code Online (Sandbox Code Playgroud)
手动修复这些是一个难题,特别是如果我修复它并继续再次编辑.
我的梦想
如果我可以在视觉上选择多行使用shit+v,然后自动分配每行的单词数,那将是完美的.
我在Python文档字符串的末尾看到了一条尾随的白线(比如在Numpy文档字符串和Google风格的文档字符串中,我记得在某处读到了这是建议的.
这就是说,它们似乎在文档字符串转换器,如被忽略在Napolean与在常规实例中的PEP-257文档.
在Python文档字符串中添加尾部白线的目的是什么?它纯粹是为了易读吗?
将较长的语句附加到列表时,我觉得append阅读起来很尴尬.我想要一个适用于动态列表创建的方法(即不需要先用零初始化等等),但我似乎无法想出另一种方法来做我想做的事情.
例:
import math
mylist = list()
phi = [1,2,3,4] # lets pretend this is of unknown/varying lengths
i, num, radius = 0, 4, 6
while i < num:
mylist.append(2*math.pi*radius*math.cos(phi[i]))
i = i + 1
Run Code Online (Sandbox Code Playgroud)
虽然append工作得很好,但我觉得它不太清楚:
mylist[i] = 2*math.pi*radius*math.cos(phi[i])
Run Code Online (Sandbox Code Playgroud)
但是这不起作用,因为该元素在列表中不存在,产生:
IndexError: list assignment index out of range
Run Code Online (Sandbox Code Playgroud)
我可以将结果值分配给临时变量,并追加它,但这看起来很丑陋且效率低下.
我有两个排序的数组,一个包含因子(数组a),当与另一个数组(数组b)的值相乘时,产生所需的值:
a(idx1) * b(idx2) = value
Run Code Online (Sandbox Code Playgroud)
有了idx2名气,我想找到idx1的a,提供必要获得尽可能接近的因素value成为可能.
我已经看过一些不同的算法(比如这个算法),但我觉得在我的特定情况下它们都会遇到浮点运算的潜在问题.
任何人都可以建议一种避免这种情况的方法吗