小编Jiv*_*van的帖子

带有lookbehind和lookahead的Python正则表达式不起作用

我有一个 Python在debuggexregex中运行良好:

在此输入图像描述

但是,当我在 Python 控制台中执行此操作时:

import re
rgx = re.compile(r'(?<="careerJobAdsList", ){"jobAds":.*}](?=,"nodes":)')

st = 'widget("careerJobAdsList", {"jobAds":[{"id":607}],"nodes":[{"id":2,"parent_id"'

rgx.match(st)
>>> None
Run Code Online (Sandbox Code Playgroud)

我尝试转义 中的所有特殊字符rgx,但它不会改变输出。

我错过了什么吗?

python regex

2
推荐指数
1
解决办法
1795
查看次数

分解Haskell中的守卫

这段代码需要一笔钱,Int并返回一个元组列表,表示所需金额或金币所需的最小金额.

purse :: Int -> [(String, Int)]
purse x
  | x == 0       = [("$0", 0)]
  | div x 200 >= 1  = before x 200 : after x 200
  | div x 100 >= 1  = before x 100 : after x 100
  | div x 50 >= 1   = before x 50 : after x 50
  | div x 20 >= 1   = before x 20 : after x 20
  | div x 10 >= 1 …
Run Code Online (Sandbox Code Playgroud)

haskell

2
推荐指数
1
解决办法
56
查看次数

加入几个没有重复列的条件

加入与Spark的标识导致公共密钥列在最终的数据集中重复:

val result = ds1.join(ds2, ds1("key") === ds2("key"))
// result now has two "key" columns
Run Code Online (Sandbox Code Playgroud)

这可以通过使用Seq而不是比较来避免,类似于USINGSQL中的关键字:

val result = ds1.join(ds2, Seq("key"))
// result now has only one "key" column
Run Code Online (Sandbox Code Playgroud)

但是,当使用公共密钥+另一个条件加入时,这不起作用,例如:

val result = ds1.join(ds2, ds1("key") === ds2("key") && ds1("foo") < ds2("foo"))
// result has two "key" columns

val result = ds1.join(ds2, Seq("key") && ds1("foo") < ds2("foo"))
// compile error: value && is not a member of Seq[String]
Run Code Online (Sandbox Code Playgroud)

目前摆脱这种情况的一种方法是在之后删除重复的列,但这非常麻烦:

val result = ds1.join(ds2, ds1("key") === ds2("key") && …
Run Code Online (Sandbox Code Playgroud)

scala apache-spark

2
推荐指数
1
解决办法
372
查看次数

在 Haskell 的 do 上下文中应用构造函数

鉴于以下声明

data MyCustomString = MyCustomString String deriving (Show, Eq)

getSomeString :: IO String
Run Code Online (Sandbox Code Playgroud)

我想在上下文中将 的输出处理getSomeString为 a :IO MyCustomStringdo

do
    cs_one <- getSomeString
    cs_two <- getSomeString

    -- at this point, `cs_one` and `cs_two` are of type `String`
    -- how to construct them into `MyCustomString` instead with no additional lines?
Run Code Online (Sandbox Code Playgroud)

但是,getSomeString返回 anString而不是MyCustomString。我怎样才能使用MyCustomString构造函数,使cs_onecs_two都在代码中的这一点上是类型MyCustomString

我试过这个无济于事:

-- this does not work
do
    MyCustomString cs_one <- getSomeString
    MyCustomString …
Run Code Online (Sandbox Code Playgroud)

constructor haskell

2
推荐指数
2
解决办法
51
查看次数

Postgresql 中的时间窗口滚动总和

我想知道是否可以在 Postgresql 中使用基于时间的窗口查询。

\n

原始数据位于前三列(日期、销售员、金额):

\n
\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n \n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n \n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n
日期推销员数量3 滚动天数总和
2020-01-01约翰1010
2020-01-02约翰1525
2020-01-03约翰833
2020-01-04约翰1235
2020-01-05约翰1131
2020-01-01丹尼尔55
2020-01-02丹尼尔611
2020-01-03丹尼尔718
2020-01-04丹尼尔821
2020-01-05丹尼尔924
\n
\n

第四列表示该销售员在过去三个滚动日内的总金额。

\n

Pandas 有内置函数可以做到这一点,但我想不出任何方法可以使用内置sum() over ()语法在 Postgresql 中做到这一点。我能够做到的唯一方法是使用横向连接和子查询的复杂组合以及时间增量比较的条件,至少可以说这是不优雅的。

\n

Pandas\' 方式(根据记忆,确切的语法可能略有不同) \xe2\x80\x94 无法获得任何简洁的信息:

\n
df.groupby(\'salesman\').rolling(\'3d\').sum()\n
Run Code Online (Sandbox Code Playgroud)\n

postgresql time-series window-functions rolling-computation

2
推荐指数
1
解决办法
2717
查看次数

构造两个列表的笛卡尔积,每对添加一个随机数

我正在尝试获得以下输出:

[(1,5,54), (1,6,34), (1,7,16), (2,5,59), (2,6,29), (2,7,71), (3,5,44), (3,6,67), (3,7,24)]
Run Code Online (Sandbox Code Playgroud)

这是笛卡尔积,[1..3], [5..7]其中每个结果元组都获得一个随机数作为其第三个元素。

我试过下面的代码

[(1,5,54), (1,6,34), (1,7,16), (2,5,59), (2,6,29), (2,7,71), (3,5,44), (3,6,67), (3,7,24)]
Run Code Online (Sandbox Code Playgroud)

问题是这会生成一个随机数并一直重复(假设这次是 42):

[(1,5,42), (1,6,42), (1,7,42), (2,5,42), (2,6,42), (2,7,42), (3,5,42), (3,6,42), (3,7,42)]
Run Code Online (Sandbox Code Playgroud)

然后我想,当然,让我们实际生成length [1..3] * length [5..7]数字:

hello :: IO [(Int, Int, Int)]
hello = do
    g <- getStdGen
    let zyx = do
            x <- [1..3]
            y <- [5..7]
            z <- take 1 $ randomRs (1, 100) g
            pure (x, y, z)
    pure zyx
Run Code Online (Sandbox Code Playgroud)

但现在这将随机数列表组合到产品中: …

random haskell list

2
推荐指数
1
解决办法
46
查看次数

PyTorch Lightning 子模型列表不会传输到 GPU

在 CPU 上使用 PyTorch Lightning 时,一切正常。然而,当使用 GPU 时,我得到一个RuntimeError: Expected all tensors to be on the same device.

问题似乎来自于使用未传递给 GPU 的子模型列表的模型:

class LambdaLayer(LightningModule):
    def __init__(self, fun):
        super(LambdaLayer, self).__init__()
        self.fun = fun

    def forward(self, x):
        return self.fun(x)

class TorchModel(LightningModule):
    def __init__(self):
        super(TorchModel, self).__init__()
        self.cat_layers = [TorchCatEmbedding(cat) for cat in columns_to_embed]
        self.num_layers = [LambdaLayer(lambda x: x[:, idx:idx+1]) for _, idx in numeric_columns]
        self.ffo = TorchFFO(len(self.num_layers) + sum([embed_dim(l) for l in self.cat_layers]), y.shape[1])
        self.softmax = torch.nn.Softmax(dim=1)

model = TorchModel()
trainer = Trainer(gpus=-1) …
Run Code Online (Sandbox Code Playgroud)

python gpu pytorch pytorch-lightning

2
推荐指数
1
解决办法
2677
查看次数

在 Rust 中为 lazy_static 赋予正确的类型

我有这个文件utils.rs声明lazy_static如下:

extern crate geojson;
extern crate geo;

use self::geo::MultiPolygon;
use self::geojson::GeoJson;
use self::geojson::conversion::TryInto;

lazy_static! {
    pub static ref LAND_POLYGONS: MultiPolygon<f64> = {
        let input_string = include_str!("resources/land.geojson");
        let mut polygons: Vec<Polygon<f64>> = vec![];
        // ...
        // add some polygons here
        // ...
        MultiPolygon(polygons)
    };
}
Run Code Online (Sandbox Code Playgroud)

然后在main.rs我尝试使用LAND_POLYGONS如下:

#[macro_use]
extern crate lazy_static;

extern crate geo;

use self::geo::MultiPolygon;
use utils::LAND_POLYGONS;

fn main() -> Result<(), Box<Error>> {
    lazy_static::initialize(&LAND_POLYGONS);
    println!("{:?}", LAND_POLYGONS);

    Ok(())
}
Run Code Online (Sandbox Code Playgroud)

这会产生以下编译器错误:

error[E0277]: `utils::LAND_POLYGONS` doesn't implement …
Run Code Online (Sandbox Code Playgroud)

static rust

1
推荐指数
1
解决办法
1377
查看次数

使用Pandas根据另一列的值选择一列

熊猫数据框的结构如下:

              color    price_blue   price_red   price_green
date
2019-01-01    green            10          20            30
2019-01-02      red            40          50            60
2019-01-03     blue            11          12            13
Run Code Online (Sandbox Code Playgroud)

我们如何price基于color每一行的列值添加一列:

              color    price_blue   price_red   price_green    price
date
2019-01-01    green            10          20            30       30
2019-01-02      red            40          50            60       50
2019-01-03     blue            11          12            13       11
Run Code Online (Sandbox Code Playgroud)

尽管apply类似的解决方案似乎很容易,但是在这种情况下,由于数据集相对较大,因此需要向量化解决方案。

此外,类似的解决方案np.where仅需很少的颜色选择就可以很好地工作,但是在实际情况下,存在20多种不同的“颜色”,因此最终由于嵌套过多而变得不切实际where

python numpy pandas

1
推荐指数
1
解决办法
32
查看次数

Haskell 中的类型重用

我试图让类型Shape重新使用类型CircleRectangle

data Point = Point {
    x :: Float,
    y :: Float
} deriving (Show)

data Circle = Circle {
    c :: Point,
    r :: Float
} deriving (Show)

data Rectangle = Rectangle {
    p1 :: Point,
    p2 :: Point
} deriving (Show)

-- the following obviously doesn't compile
data Shape = Circle Circle | Rectangle Rectangle deriving (Show)
Run Code Online (Sandbox Code Playgroud)

以上导致编译错误

Multiple declarations of ‘Circle’
18 | data Shape = Circle Circle | Rectangle Rectangle deriving …
Run Code Online (Sandbox Code Playgroud)

haskell types

1
推荐指数
1
解决办法
57
查看次数