我想在matplotlib图中的轴标签中使用下标.使用LaTeX我会将其设置为$N_i$,这给了我斜体衬线字体.我知道我可以使用非斜体数学\mathrm.但我想在默认的matplotlib sans-serif字体中获取文本,以便它匹配图中其余文本.有没有办法下标文本而不使用乳胶?
我有一个包含 JSON 数组的列的表。举个例子:
with example as (
select '["a", "b"]'::jsonb as col
union select ('["c", "d", "e"]'::jsonb) as col
)
select col from example
Run Code Online (Sandbox Code Playgroud)
返回:
col
["a", "b"]
["c", "d", "e"]
Run Code Online (Sandbox Code Playgroud)
我可以用 jsonb_array_elements将每个数组扩展为行:
select jsonb_array_elements(col) from example
Run Code Online (Sandbox Code Playgroud)
返回:
jsonb_array_elements
"a"
"b"
"c"
"d"
"e"
Run Code Online (Sandbox Code Playgroud)
我想要每个数组元素的索引以及元素本身(有点像 Python 的 enumerate),如下所示:
jsonb_array_elements array_index
"a" 1
"b" 2
"c" 1
"d" 2
"e" 3
Run Code Online (Sandbox Code Playgroud)
我怎样才能做到这一点?
我的应用程序具有只读访问权限,因此我无法创建函数。
我正在尝试解决Scala中的"简单"练习.我有这个功能:
def mean(xs: Seq[Double]): Option[Double] =
if (xs.isEmpty) None
else Some(xs.sum / xs.length)
Run Code Online (Sandbox Code Playgroud)
这是练习的内容:
练习2:根据均值和平面图实现方差函数(如果均值为m,方差是math.pow(x - m,2)的平均值,请参见定义).
我在想类似的东西
val xs = List(1.3,2.1,3.2)
val m = mean(xs)
xs flatMap(x=> math.pow(x-m,2)).mean //error!
Run Code Online (Sandbox Code Playgroud)
什么是正确的解决方法?如果可能的话,我也想要一个小的理论解释
在下面,我定义类型变量,泛型类型别名和点积函数。mypy不会引发错误。为什么不?
我希望它抛出一个错误v3,因为它是一个字符串矢量,我已经明确指出T必须是int,float或complex。
from typing import Any, Iterable, Tuple, TypeVar
T = TypeVar('T', int, float, complex)
Vector = Iterable[T]
def dot_product(a: Vector[T], b: Vector[T]) -> T:
return sum(x * y for x, y in zip(a, b))
v1: Vector[int] = [] # same as Iterable[int], OK
v2: Vector[float] = [] # same as Iterable[float], OK
v3: Vector[str] = [] # no error - why not?
Run Code Online (Sandbox Code Playgroud) 下面是我遇到的一个问题的简化示例mypy。该A.transform方法采用对象的可迭代对象,转换每个对象(在子类中定义B,并可能在其他子类中定义)并返回已转换对象的可迭代对象。
from typing import Iterable, TypeVar
T = TypeVar('T')
class A:
def transform(self, x: Iterable[T]) -> Iterable[T]:
raise NotImplementedError()
class B(A):
def transform(self, x: Iterable[str]) -> Iterable[str]:
return [x.upper() for x in x]
Run Code Online (Sandbox Code Playgroud)
然而mypy说:
error: Argument 1 of "transform" incompatible with supertype "A"
error: Return type of "transform" incompatible with supertype "A"
Run Code Online (Sandbox Code Playgroud)
如果我[T]从 中删除A.transform(),那么错误就会消失。但这似乎是错误的解决方案。
在阅读了covariance 和 contravariance 之后,我认为设置
T = TypeVar('T', covariant=True)可能是一个解决方案,但这会产生相同的错误。
我怎样才能解决这个问题?我已经考虑过将设计完全合并并用更高阶的函数替换 A 类。
mypy ×2
python ×2
types ×2
fonts ×1
json ×1
latex ×1
matplotlib ×1
postgresql ×1
python-3.x ×1
scala ×1
type-hinting ×1