繁体   English   中英

如何删除 R/exams 中网状输出中的括号?

[英]How to remove brackets in a reticulate output in R/exams?

我有以下单选练习RadicalPrimo.Rmd ,旨在简化平方根:

```{r data generation, echo = FALSE, results = "hide"}
library("exams")
library("reticulate")

# use_python('/usr/bin/python3', required = TRUE)
use_python('/home/rstudio/.local/share/r-miniconda/envs/r-reticulate/bin/python', required = TRUE)
options(scipen=999)
typ <- match_exams_device()
#--------------------------------------------------------
```

```{python, include=FALSE}
from sympy import *   
init_printing()     ##### Facilita la impresión en un formato legible
```

```{python, eval = TRUE, echo = FALSE,results='asis'}
import sympy
import random
nrand1 = random.randint(2, 200)
nram = nrand1*2
sol = sympy.sqrt(nrand1)
pp = print('$' + sympy.printing.latex(sol) + '$')
```

Question
========

Simplify: 

$$\sqrt{`r py$nrand1`}$$


Answerlist
----------

*   
```{python, eval = TRUE, echo = FALSE,results='asis'}
import sympy
print('$' + sympy.printing.latex(sol) + '$')
```

*   
```{python, eval = TRUE, echo = FALSE,results='asis'}
import sympy
import random 
nrand2 = random.randint(1, 200)
if (nrand2 != nrand1) & (nrand2<nrand1):
  inc2 = sympy.sqrt(nrand2)
  inc2b = sympy.root(nrand2,2)
  inc =random.sample((inc2,inc2b),1)
print('$' + sympy.printing.latex(inc) + '$')
```

*  
```{python, eval = TRUE, echo = FALSE,results='asis'}
import sympy
import random
nrand3 = random.randint(2, 200)
if (nrand2 != nrand1) & (nrand3 != nrand2) & (nrand3<nrand1):
  inc3 = sympy.sqrt(nrand3)
pp3 = print('$' + sympy.printing.latex(inc3) + '$')
```

*  
```{python, eval = TRUE, echo = FALSE,results='asis'}
import sympy
import random
nrand4 = random.randint(2, 200)
if (nrand2 != nrand1) & (nrand3 != nrand2) & (nrand4 != nrand3) & (nrand4<nrand1):
  inc4 = sympy.sqrt(nrand4)
pp4 = print('$' + sympy.printing.latex(inc4) + '$')
```

Solution
========

```{python, eval = TRUE, echo = FALSE,results='asis'}
import sympy
print('$' + sympy.printing.latex(sol) + '$')
``` 

Meta-information
================
exname:RadicalPrimo(single-choice)
extype:schoice
exsolution: 1000
exshuffle: TRUE

但是,选择列表中的一些答案带有不雅的方括号:

在此处输入图像描述

我已经应用了各种方法来删除 Python 输出中的括号,但无济于事。 我怎样才能永久删除这些括号?

在您的示例inc中, random.sample((inc2,inc2b),1)的输出是 Python 中的list 因此,在打印时(无论是在 Python 中还是作为 LaTeX 中),都会添加括号:

>>> type(inc)
## <class 'list'>
>>> inc
## [sqrt(67)]
>>> sympy.printing.latex(inc)
## '\\left[ \\sqrt{67}\\right]'

您可以简单地提取第一个元素并打印:

>>> inc = inc[0]
>>> type(inc)
## <class 'sympy.core.power.Pow'>
>>> inc
## sqrt(67)
>>> sympy.printing.latex(inc)
## '\\sqrt{67}'

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM