在Nim中编写和读取二进制文件的最佳方法是什么?我想将交替的浮点数和整数写入二进制文件,然后能够读取该文件.要用Python编写这个二进制文件,我会做类似的事情
import struct
# list of alternating floats and ints
arr = [0.5, 1, 1.5, 2, 2.5, 3]
# here 'f' is for float and 'i' is for int
binStruct = struct.Struct( 'fi' * (len(arr)/2) )
# put it into string format
packed = binStruct.pack(*tuple(arr))
# open file for writing in binary mode
with open('/path/to/my/file', 'wb') as fh:
fh.write(packed)
Run Code Online (Sandbox Code Playgroud)
要阅读我会做的事情
arr = []
with open('/path/to/my/file', 'rb') as fh:
data = fh.read()
for i in range(0, len(data), 8):
tup = binStruct.unpack('fi', …Run Code Online (Sandbox Code Playgroud) 我开始学习来自C++背景的Python.我正在寻找的是一种快速简便的方法,可以在2D(numpy)多维点数组(也是numpy数组)中找到某个多维查询点的最近(最近邻居).我知道scipy有一棵kd树,但我认为这不是我想要的.首先,我将更改2D数组中多维点的值.其次,2D阵列中每个点的位置(坐标)很重要,因为我也将改变它们的邻居.
我可以编写一个通过2D数组的函数,测量查询点和数组中的点之间的距离,同时跟踪最小的一个(使用scipy空间距离函数来测量距离).是否有内置功能可以做到这一点?我试图尽可能避免在python中迭代数组.我还将有许多查询点,因此至少有两个"for循环" - 一个遍历查询点,每个查询循环迭代2D数组并找到最小距离.
谢谢你的建议.
我使用 h5py 创建了一个包含 5000 个组的 hdf5 文件,每个组包含 1000 个数据集。数据集是一维整数数组,平均大约有 10,000 个元素,尽管这可能因数据集而异。创建数据集时没有使用分块存储选项。数据集的总大小为 281 GB。我想迭代所有数据集以创建一个将数据集名称映射到数据集长度的字典。稍后我将在算法中使用这本字典。
这是我尝试过的。
import h5py
f = h5py.File('/my/path/to/database.hdf5', 'r')
lens = {}
for group in f.itervalues():
for dataset in group.itervalues():
lens[dataset.name] = dataset.len()
Run Code Online (Sandbox Code Playgroud)
这对于我的目的来说太慢了,我正在寻找加快此过程的方法。我知道可以使用 h5py 并行操作,但想看看在走这条路之前是否有另一种方法。如果可以加快速度,我愿意使用不同的选项/结构重新创建数据库。
刚开始用Nim语言编程(到目前为止我真的很喜欢).作为一个学习练习,我正在编写一个小型矩阵库.我有更多代码,但我只会展示与此问题相关的部分.
type
Matrix*[T; nrows, ncols: static[int]] = array[0 .. (nrows * ncols - 1), T]
# Get the index in the flattened array corresponding
# to row r and column c in the matrix
proc index(mat: Matrix, r, c: int): int =
result = r * mat.ncols + c
# Return the element at r, c
proc `[]`(mat: Matrix, r, c: int): Matrix.T =
result = mat[mat.index(r, c)]
# Set the element at r, c
proc `[]=`(mat: var Matrix, r, …Run Code Online (Sandbox Code Playgroud) 我在Nim(版本0.10.2)中传递数学函数(procs)时遇到问题.
import math
var s1 = @[1.1, 1.2, 1.3, 1.4]
var s2 = map(s1, math.sqrt)
Run Code Online (Sandbox Code Playgroud)
我收到了错误
Error: 'sqrt' cannot be passed to a procvar
Run Code Online (Sandbox Code Playgroud)
如果我为sqrt编写一个包装器函数,它就可以正常工作.
proc fxn(x: float): float = math.sqrt(x)
var s2 = map(s1, fxn)
Run Code Online (Sandbox Code Playgroud)
我使用平方根和map作为示例,但最终我将sqrt(和其他数学过程)传递给另一个proc.有没有办法在不编写包装函数的情况下执行此操作?
假设我有两个类和一个以相同方式修改任一类的过程。如何指定参数可以是类(而不是为每个类重写或重载函数)?一个简单的例子:
type
Class1[T] = object
x: T
Class2[T] = object
x: T
y: T
# this works fine
proc echoX[T](c: Class1[T]|Class2[T]) =
echo c.x
# this does not work
proc addToX[T](c: var Class1[T]|Class2[T], val: T) =
c.x += val
var c1: Class1[int]
var c2: Class2[int]
# this works fine
echoX(c1)
echoX(c2)
# this does not work
addToX(c1, 10)
addToX(c2, 100)
Run Code Online (Sandbox Code Playgroud)
我收到以下错误。
Error: for a 'var' type a variable needs to be passed
Run Code Online (Sandbox Code Playgroud)
如果我为每个类使用单独的过程,则一切正常。
proc addToX[T](c: var Class1[T], val: T) = …Run Code Online (Sandbox Code Playgroud) 寻找一种使用Nim编程语言(版本0.11.2)从tar.gz存档读取文件的方法.说我有一个档案
/my/path/to/archive.tar.gz
Run Code Online (Sandbox Code Playgroud)
和该档案中的文件
my/path/to/archive/file.txt
Run Code Online (Sandbox Code Playgroud)
我的目标是能够在Nim中逐行读取文件的内容.在Python中,我可以使用tarfile模块执行此操作.在Nim中有libzip和zlib模块,但文档很少,没有示例.还有zipfiles模块,但我不确定它是否能够使用tar.gz档案.
我有一个带有构造函数的Vector类
Vector(int dimension) // creates a vector of size dimension
Run Code Online (Sandbox Code Playgroud)
我有一个类Neuron,它扩展了Vector类
public class Neuron extends Vector {
public Neuron(int dimension, ... other parameters in here ...) {
super(dimension);
// other assignments below here ...
}
}
Run Code Online (Sandbox Code Playgroud)
我希望能够做的是在Neuron类中为Vector指定另一个Vector的引用.有点像
public Neuron(Vector v, ... other parameters in here ...) {
super = v;
// other assignments below here ...
}
Run Code Online (Sandbox Code Playgroud)
当然,我不能这样做.有一些工作吗?即使我无法在Neuron类的构造函数中执行此操作,也许可以.
我正在使用 matplotlib-venn 包在 python 中绘制维恩图。这个包非常适合用两到三组绘制维恩图。但是,当其中一组比其他组大得多时,较小圆圈中的计数可能会接近或重叠。这是一个例子。
from collections import Counter
import matplotlib.pyplot as plt
from matplotlib_venn import venn2, venn3
sets = Counter()
sets['01'] = 3000
sets['11'] = 3
sets['10'] = 5
setLabels = ['set1', 'set2']
plt.figure()
ax = plt.gca()
v = venn2(subsets = sets, set_labels = setLabels, ax = ax)
plt.title('Venn Diagram')
plt.show()
Run Code Online (Sandbox Code Playgroud)
我要做的是将计数(在本例中为 3000、3 和 5)移动到颜色与图中颜色匹配的图例中。不知道如何用 matplotlib_venn 做到这一点。
nim-lang ×5
python ×2
binaryfiles ×1
file ×1
gzip ×1
hdf5 ×1
inheritance ×1
io ×1
java ×1
matplotlib ×1
numpy ×1
reference ×1
scipy ×1
superclass ×1
tar ×1