我遇到了这种奇怪的行为,我找不到解释。
MWE:
l = [1]
l += {'a': 2}
l
[1, 'a']
l + {'B': 3}
Traceback (most recent call last):
File "<input>", line 1, in <module>
TypeError: can only concatenate list (not "dict") to list
Run Code Online (Sandbox Code Playgroud)
基本上,当我+=python 不会引发错误并将密钥附加到列表时,而当我只计算时,+我得到了预期的TypeError.
注意:这是 Python 3.6.10
这个问题可能过多,但我找不到.基本上我使用RStudio和键盘快捷键cmd+ shift+ c来插入注释.还有其他组合直接插入roxygen标签#'吗?还是一种修改RStudio的方法告诉它添加'我按cmd+ shift+的时候c?
有没有办法从包中自动导入所有隐藏的功能,即只能使用package:::fun?
事实上,我已经对一个使用了很多内部函数的给定函数进行了一些修改,我想避免package:::在任何地方重新输入.
我查看了loadNamespace基本函数,但它没有附加未导出的函数.
当我有一个pd.DataFramewith 路径时,我最终做了很多.map(lambda path: Path(path).{method_name},或者apply(axis=1)例如:
(
pd.DataFrame({'base_dir': ['dir_A', 'dir_B'], 'file_name': ['file_0', 'file_1']})
.assign(full_path=lambda df: df.apply(lambda row: Path(row.base_dir) / row.file_name, axis=1))
)
base_dir file_name full_path
0 dir_A file_0 dir_A/file_0
1 dir_B file_1 dir_B/file_1
Run Code Online (Sandbox Code Playgroud)
这对我来说似乎很奇怪,尤其是因为pathlib确实实现了,/所以类似的东西df.base_dir / df.file_name会更加 Pythonic 和自然。
我还没有找到path在 Pandas 中实现的任何类型,有什么我遗漏的吗?
我发现最好一次做一次,astype(path)然后至少对路径连接进行pathlib矢量化:
(
pd.DataFrame({'base_dir': ['dir_A', 'dir_B'], 'file_name': ['file_0', 'file_1']})
# this is where I would expect `astype({'base_dir': Path})`
.assign(**{col_name:lambda df: df[col_name].map(Path) for col_name …Run Code Online (Sandbox Code Playgroud) 我正在使用循环进行一些集成integrate,但出现了一个我无法理解的错误,也无法摆脱。这是我可以提取的 MWE:
u_min = 0.06911363
u_max = 1.011011
m = 0.06990648
s = 0.001092265
integrate(f = function(v){pnorm(v, mean = m, sd = s, lower.tail = FALSE)}, u_min, u_max)
Run Code Online (Sandbox Code Playgroud)
这会返回一个错误“积分可能发散”,这显然是错误的。我尝试稍微修改一下参数并使其正常工作,例如:
u_min <- 0.07
u_max <- 1.1
m <- 0.0699
s <- 0.00109
integrate(f = function(v){pnorm(v, mean = m, sd = s, lower.tail = FALSE)}, u_min, u_max)
Run Code Online (Sandbox Code Playgroud)
我试图查看该integrate函数,debug但它是代码的包装C。而且我也不是正交技术的专家。我看到了这篇帖子,但无法从中得到任何东西。
谢谢
我正在为R包mistral构建小插图(请参见github.com/clemlaflemme/mistral),并devtools::check(cleanup = FALSE)返回警告
Error: processing vignette 'mistral-vignette.Rmd' failed with diagnostics:
4 simultaneous processes spawned
Execution halted
Run Code Online (Sandbox Code Playgroud)
我在Google上找不到任何帮助。实际上,我确实在小插图中使用了并行计算,这是不允许的吗?
我找到了几个关于如何处理kwargsMyPy 的答案。但实际上我的问题是 mypy 没有正确捕获这一点:
from typing import Union
class Parent:
def __init__(self, a: int, b: str) -> None:
self.a = a
self.b = b
class Child(Parent):
def __init__(self, c: float, **kwargs: Union[int, str]) -> None:
super().__init__(**kwargs)
self.c = c
child = Child(a="a", b=2, c=2.3)
# tmp.py:12: error: Argument 1 to "__init__" of "Parent" has incompatible type "**Dict[str, Union[int, str]]"; expected "int"
# tmp.py:12: error: Argument 1 to "__init__" of "Parent" has incompatible type "**Dict[str, Union[int, str]]"; expected "str" …Run Code Online (Sandbox Code Playgroud) 我最近发现了knitr,我主要用它来制作dev=tikz用于轻松进行乳胶排版的图.但是我不明白如何设置块选项fig.width和out.width一致性.
我的意思是,我知道第一个是R选项,而第二个是乳胶选项,\includegraphics但似乎如果fig.width太大,out.width那么线条非常薄,因为乳胶缩小了图片.另一方面,如果它太小,那么乳胶会拉伸它,而且一切都太大了.
基本上我也想有一个设置,我只选择out.width,然后行和文本的厚度与文档的文本大小一致.
我包括一个MWE来说明我的问题.另外我已经设置fig.height了第一个例子,否则图片比页面大,我也不太明白这一点.
热烈欢迎任何帮助!
\documentclass[12pt,a4paper]{article}
\usepackage[utf8]{inputenc}
\usepackage[english]{babel}
\usepackage[T1]{fontenc}
\usepackage{graphicx}
<<setup, include=FALSE, cache=FALSE>>=
library(knitr)
options(formatR.arrow=TRUE,width=50)
opts_chunk$set(fig.path='figure/graphics-',
cache.path='cache/graphics-',
fig.align='center',
dev='tikz')
@
\begin{document}
\begin{figure}[!ht]
<<fig_1,fig.width=2, fig.height = 3,out.width = '\\textwidth',echo=FALSE>>=
x = seq(1,3,l=100)
plot(x,cos(x), type ="l", xlab = "x", ylab = "y")
@
\end{figure}
\begin{figure}[!ht]
<<fig_2,fig.width=10, out.width = '\\textwidth',echo=FALSE>>=
x = seq(1,3,l=100)
plot(x,cos(x), type ="l", xlab = "x", ylab = "y")
@
\end{figure}
\end{document}
Run Code Online (Sandbox Code Playgroud) 我想ggplot2只将科学记数法的第一部分写到轴上,然后在轴的顶部添加一个 $x 10^n$ 数量级。有没有一个功能可以做到这一点?
这是一个带有 hack 的 MWE 来说明我的意思:
ggplot(data = data.frame(x = 1:10, y = seq(1, 2, l = 10)*1000), aes(x,y)) + geom_line()
Run Code Online (Sandbox Code Playgroud)
而我想要的是:
ggplot(data = data.frame(x = 1:10, y = seq(1, 2, l = 10)*1000), aes(x,y)) + geom_line() +
scale_y_continuous(breaks = c(1, 1.25, 1.5, 1.75, 2, 2.05)*1000, label = c(1, 1.25, 1.5, 1.75, 2, "x 10^3"))
Run Code Online (Sandbox Code Playgroud)
作为一个附带问题,我注意到当刻度标签很大时,轴标签会很快接近刻度标签。有没有办法在它们之间设置自动间距?