我有一个元素列表,比方说
list = [1, 2, 3, 4]
Run Code Online (Sandbox Code Playgroud)
我想迭代这个列表中不同元素的一对,所以
for x, y in some_iterator(list):
print x, y
Run Code Online (Sandbox Code Playgroud)
应该表明
1 2
1 3
1 4
2 3
2 4
3 4
Run Code Online (Sandbox Code Playgroud)
请注意,我不希望list在此问题中使用所有组合.只是给定长度的组合.
这样做的最pythonic方式是什么?
如果我想对n-uples做同样的事情怎么办?例如,3元素的组合n
for x, y, z in another_iterator(list):
print x, y, z
Run Code Online (Sandbox Code Playgroud)
会表现出来
1 2 3
1 2 4
2 3 4
Run Code Online (Sandbox Code Playgroud) 我打算对Minix内核进行修改.但在我开始之前,我想编译它,以便我知道任何进一步的编译问题都是由我所做的事情引起的.
我从github获得了Minix 3源代码,在那里它被镜像:
git clone git://github.com/minix3/minix
Run Code Online (Sandbox Code Playgroud)
现在,我希望在对代码进行任何修改之前编译它.当我make进去的时候src,我收到了消息
Makefile:109: *** missing separator. Stop.
Run Code Online (Sandbox Code Playgroud)
我尝试通过执行make来编译内核src/minix/kernel.在这种情况下,我收到消息
Makefile:2: *** missing separator. Stop.
Run Code Online (Sandbox Code Playgroud)
我该如何规避这个问题?如何编译Minix源?
当我用鼠标选择文本时,我可以使用中键单击粘贴它。除了在 Jupyter 笔记本中。这显然是由于 codemirror 造成的,但该问题已由codemirror 解决。这是否意味着现在有一种方法可以让中键单击粘贴在 Jupyter 中工作?
在 Fedora 31 上,我尝试使用 pip 安装 mayavi
pip install mayavi
Run Code Online (Sandbox Code Playgroud)
但是,出现以下错误
Building wheel for mayavi (setup.py) ... error
ERROR: Command errored out with exit status -11:
command: /usr/bin/python3 -u -c 'import sys, setuptools, tokenize; sys.argv[0] = '"'"'/tmp/pip-install-4s7scony/mayavi/setup.py'"'"'; __file__='"'"'/tmp/pip-install-4s7scony/mayavi/setup.py'"'"';f=getattr(tokenize, '"'"'open'"'"', open)(__file__);code=f.read().replace('"'"'\r\n'"'"', '"'"'\n'"'"');f.close();exec(compile(code, __file__, '"'"'exec'"'"'))' bdist_wheel -d /tmp/pip-wheel-ggg3s_hz
cwd: /tmp/pip-install-4s7scony/mayavi/
Complete output (55 lines):
running bdist_wheel
running build
----------------------------------------------------------------------
Building TVTK classes... vtkContextDevice2D: Ignoring method: Get/SetViewportRect
default: [0, 0, 0, 0], range: None
vtkContextDevice2D: Ignoring method: Get/SetViewportSize
default: [0, 0], range: None …Run Code Online (Sandbox Code Playgroud) 我有一个 3d 数组
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.widgets import Slider
A = np.random.rand(10,5,5)
Run Code Online (Sandbox Code Playgroud)
我想将每个 5x5 图像可视化A
for Ai in A:
plt.imshow(Ai)
plt.show()
Run Code Online (Sandbox Code Playgroud)
除了像上面的例子那样有 5 个数字之外,我想要一个滑块来在 的第一个坐标的索引之间切换A。
目前,我已尝试以下操作:
idx0 = 3
l = plt.imshow(A[idx0])
axidx = plt.axes([0.25, 0.15, 0.65, 0.03])
slidx = Slider(axidx, 'index', 0, 9, valinit=idx0, valfmt='%d')
def update(val):
idx = slidx.val
l.set_data(A[idx])
fig.canvas.draw_idle()
slidx.on_changed(update)
plt.show()
Run Code Online (Sandbox Code Playgroud)
但是当我使用滑块时,图中的图像不会改变,并且我收到消息
IndexError: only integers, slices (`:`), ellipsis (`...`), numpy.newaxis (`None`) and integer or boolean arrays are …Run Code Online (Sandbox Code Playgroud) 我试图使用 scipy 来估计数据集在某些点的密度。
from scipy.stats import gaussian_kde
import numpy as np
Run Code Online (Sandbox Code Playgroud)
我有一个A3D 点数据集(这只是一个最小的例子。我的实际数据有更多维度和更多样本)
A = np.array([[0.078377 , 0.76737392, 0.45038174],
[0.65990129, 0.13154658, 0.30770917],
[0.46068406, 0.22751313, 0.28122463]])
Run Code Online (Sandbox Code Playgroud)
以及我想要估计密度的点
B = np.array([[0.40209377, 0.21063273, 0.75885516],
[0.91709997, 0.79303252, 0.65156937]])
Run Code Online (Sandbox Code Playgroud)
但我似乎无法使用该gaussian_kde功能,因为
result = gaussian_kde(A.T)(B.T)
Run Code Online (Sandbox Code Playgroud)
回报
LinAlgError: Matrix is not positive definite
Run Code Online (Sandbox Code Playgroud)
我该如何修复这个错误?如何获得样品的密度?
在Python中,我想做这样的事情
an_explicit_variable_name, another_explicit_variable_name, an_even_more_explicit_variable_name = function(foo)
Run Code Online (Sandbox Code Playgroud)
但我也希望这个可读并适合几条短线而不是一条非常长的线.我没有在PEP 08找到任何帮助.这个问题有点关系,但答案并不是我想要的.
就线长而言,这是我能想到的最好的,但我并不喜欢使用a[0]等等.
a = function(foo)
an_explicit_variable_name = a[0]
another_explicit_variable_name = a[1]
an_even_more_explicit_variable_name = a[2]
Run Code Online (Sandbox Code Playgroud)
这不起作用,因为第二行仍然太长
_ = function(foo)
an_explicit_variable_name, another_explicit_variable_name, an_even_more_explicit_variable_name = _
Run Code Online (Sandbox Code Playgroud)
我应该将我在多行中声明的变量分开吗?如果是这样,我该如何缩进?
an_explicit_variable_name, \
another_explicit_variable_name, \
an_even_more_explicit_variable_name \
= function(foo)
an_explicit_variable_name, \
another_explicit_variable_name, \
an_even_more_explicit_variable_name \
= function(foo)
Run Code Online (Sandbox Code Playgroud)
在这种情况下采用什么样的风格?
我正在尝试在 Python 中创建 FITS 文件,但在将标头和 PrimaryHDU 一起编译时似乎遇到问题。
我做了一个简单的例子,它会给出我收到的错误:
import numpy as np
from astropy.io import fits
a = np.ones([5,5])
hdu = fits.PrimaryHDU(a)
hdr = fits.Header()
hdr['NPIX1'] = 60
hdr['NPIX2'] = 60
hdr['CRPIX1'] = 0
hdr['CRPIX2'] = 0
primary_hdu = fits.PrimaryHDU(header=hdr)
hdul = fits.HDUList([primary_hdu, hdu])
hdul.writeto('table4.fits')
Run Code Online (Sandbox Code Playgroud)
运行此代码时,我收到以下错误:
verifyError:验证报告错误:HDUList 的元素 1 不是扩展 HDU。注意:astropy.io.fits 使用从零开始的索引。
我看过一些帖子,声称这可能与导出时 PrimaryHDU 需要成为 HDUList 中的第一个有关,但看看我的代码,我相信我已经这样做了。
任何帮助将不胜感激,谢谢。
astropy文档有一个关于如何从 FITS 文件绘制图像的示例。结果图上的轴对应于像素或数据点的数量。我希望轴指示银河坐标。
请注意,我的 FITS 文件的标头指示了有关图像在天空上的位置的所有信息。如何让绘图显示真实坐标而不是像素数?
我第一次尝试使用 Sphinx。我正在跑步sphinx-quickstart,但不明白被问到的第一个问题:
> Separate source and build directories (y/n) [n]:
Run Code Online (Sandbox Code Playgroud)
这是什么意思?这些目录是否分开有什么区别?我怎么知道选择哪一个?
对于我正在研究的项目,我在DEAP中设置了3个不同的目标作为优化目标(这是一个基于Python的演化框架).
它可以使用像NSGA-II这样的算法来应对多目标问题.无论如何都要生成帕累托边界曲面以显示结果.
在bash中,setenv返回“找不到命令...”。我(认为我)可以替换setenv MYPATH /path/to/something/为
MYPATH=/path/to/something/
export MYPATH
Run Code Online (Sandbox Code Playgroud)
但是我可以在Fedora中使用一个命令执行与以下命令相同的操作setenv吗?
python ×8
python-3.x ×3
astropy ×2
fits ×2
matplotlib ×2
bash ×1
codemirror ×1
coding-style ×1
copy-paste ×1
deap ×1
iterator ×1
makefile ×1
mayavi ×1
minix ×1
pip ×1
python-2.7 ×1
scipy ×1
setenv ×1