[英]How to remove brackets in a reticulate output in R/exams?
I have the following single-choice exercise RadicalPrimo.Rmd
, which is intended to simplify square roots:我有以下单选练习
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
However, some answers in the choice list have inelegant square brackets:但是,选择列表中的一些答案带有不雅的方括号:
I have applied various methods to remove brackets in Python output, but to no avail.我已经应用了各种方法来删除 Python 输出中的括号,但无济于事。 How can I permanently remove those brackets?
我怎样才能永久删除这些括号?
In your example inc
, the output of random.sample((inc2,inc2b),1)
is a list
in Python.在您的示例
inc
中, random.sample((inc2,inc2b),1)
的输出是 Python 中的list
。 Hence, when printed (both within Python or as LaTeX) the brackets are added:因此,在打印时(无论是在 Python 中还是作为 LaTeX 中),都会添加括号:
>>> type(inc)
## <class 'list'>
>>> inc
## [sqrt(67)]
>>> sympy.printing.latex(inc)
## '\\left[ \\sqrt{67}\\right]'
You can simply extract the first element and print that:您可以简单地提取第一个元素并打印:
>>> 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.