小编Jiv*_*van的帖子

在PHP中用两位数和月份名称显示月份

我有一个循环以表格形式显示月份。

我想以两种不同的格式显示月份

  1. 用这样的两位数字01,02,03 ...
  2. 使用这样的纯文本一月,二月 ...

像这样:

<option value="01">January</option>
<!-- ... -->
<option value="12">December</option>
Run Code Online (Sandbox Code Playgroud)

问题是,我无法显示此内容。

到目前为止,这是我的代码:

echo '<select name="month">';

for ($x=1; $x<=12; $x++)
{
    $val=strlen($x);

    if($val==1)
    {
        echo '<option value="'.'0'.$x.'">'.'0'.$x.'</option>';
    } else {
        echo '<option value="'.$x.'">'.$x.'</option>';
}
}

echo '</select>';
Run Code Online (Sandbox Code Playgroud)

html php

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

基于查找的车把状况

我有以下数据结构:

{
    things: [
        "desk",
        "chair",
        "pen",
        "book",
        "lamp"
    ],
    owners: [
        "Julia",
        "Sandra",
        "John",
        "Paul"
    ]
}
Run Code Online (Sandbox Code Playgroud)

工作原理:

handleblars模板:

{{#each things}}
    <p>This {{this}} belongs to {{lookup ../owners @index}}</p>
{{/each}}
Run Code Online (Sandbox Code Playgroud)

正确输出:

This desk belongs to Julia
This chair belongs to Sandra
This pen belongs to John
This book belongs to Paul
This lamp belongs to
Run Code Online (Sandbox Code Playgroud)

什么不起作用:

现在,我想添加一个条件,因为最后一个thing可能没有owner。模板将如下所示:

{{#each things}}
    {{#if lookup ../owners @index}}
        <p>This {{this}} belongs to {{lookup ../owners @index}}</p>
    {{else}}
        <p>...But this {{this}} …
Run Code Online (Sandbox Code Playgroud)

javascript handlebars.js

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

分布直方图中的加权区间

我正在寻找一种绘制分布直方图的方法,y-axis代表每个bin的项目总数(而不仅仅是计数).

以下图表中的示例:

  • 在左边,有55家代理商在20至30间房屋之间售卖
  • 在右边,销售了20至30间房屋的代理商代表了1100套房屋

在此输入图像描述

它并不像看起来那么微不足道,因为人们不能简单地将每个箱子的数量乘以箱子的价值(可能在20-30箱子中,有54个代理商卖出21个,其中1个卖出29个).

问题:

  • 这种图表的名称是什么(右边的图表)?
  • 有没有办法在本地绘制matplotlibseaborn

python matplotlib histogram seaborn

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

Python 中的两个独立异步循环

使用async/await?执行在 Python 中并行运行的两个异步循环的好方法是什么?

我也想过类似下面的代码,但不能换我围绕着如何使用头async/ await/EventLoop在这种特殊情况下。

import asyncio

my_list = []

def notify():
    length = len(my_list)
    print("List has changed!", length)

async def append_task():
    while True:
        time.sleep(1)
        await my_list.append(random.random())
        notify()

async def pop_task():
    while True:
        time.sleep(1.8)
        await my_list.pop()
        notify()

loop = asyncio.get_event_loop()
loop.create_task(append_task())
loop.create_task(pop_task())
loop.run_forever()
Run Code Online (Sandbox Code Playgroud)

预期输出:

$ python prog.py
List has changed! 1 # after 1sec
List has changed! 0 # after 1.8sec
List has changed! 1 # after 2sec
List has changed! 2 # …
Run Code Online (Sandbox Code Playgroud)

python asynchronous event-loop python-3.x python-asyncio

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

在 Presto 中按数组值分组

有一张像这样的电影表:

genre               revenue
array<string>           int
---------------------------
[Drama]               10000
[Drama, Fiction]       2000
[Fiction]               300
[Comedy]               5000
[Comedy, Fiction]       500
Run Code Online (Sandbox Code Playgroud)

如何按包含的流派对电影进行分组并进行聚合,例如:

genre       count_movies    sum_revenue
---------------------------------------
Drama                  2          12000
Fiction                3           2800
Comedy                 2           5500
Run Code Online (Sandbox Code Playgroud)

我查看了数组函数和运算符的文档,但在那个方向找不到任何东西。

负责此输出的查询可能如下所示:

SELECT
  COUNT(*) AS count_movies,
  SUM(revenue) AS sum_revenue
FROM movies
GROUP BY ARRAY_EXPLODE(genre) -- this is fake, just to illustrate the point
Run Code Online (Sandbox Code Playgroud)

sql group-by presto

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

外部数据包装器“postgres_fdw”不存在(即使存在)

使用 PostgreSQL 10.10,来自超级用户postgres

CREATE EXTENSION postgres_fdw;
GRANT USAGE ON FOREIGN DATA WRAPPER postgres_fdw TO my_user;
Run Code Online (Sandbox Code Playgroud)

然后在执行以下操作时my_user

CREATE SERVER my_server
FOREIGN DATA WRAPPER postgres_fdw
OPTIONS (...);
Run Code Online (Sandbox Code Playgroud)

显示此错误消息:

Query 1 ERROR: ERROR:  foreign-data wrapper "postgres_fdw" does not exist
Run Code Online (Sandbox Code Playgroud)

以下是当前活动的外部数据包装器的列表(来自 psql):

postgres=# \dew
                      List of foreign-data wrappers
     Name     |  Owner   |       Handler        |       Validator
--------------+----------+----------------------+------------------------
 postgres_fdw | postgres | postgres_fdw_handler | postgres_fdw_validator
(1 row)
Run Code Online (Sandbox Code Playgroud)

为什么即使在被授予之后USAGE,用户my_user仍然无法看到/使用 postgres_fdw 外部数据包装器,就好像后者不存在一样?还需要采取更多步骤吗?

sql postgresql foreign-data-wrapper postgresql-10

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

处理 Haskell 中的歧义事件

我正在导入Data.TextData.List,它们各自的find功能崩溃了。

\n
import Data.Text\nimport Data.List\n\nmy_list = [4, 2, 4, 5, 6, 2, 6]\nfind (\\a -> a == 5) my_list\n
Run Code Online (Sandbox Code Playgroud)\n

上面的代码会导致以下错误

\n
    Ambiguous occurrence \xe2\x80\x98find\xe2\x80\x99\n    It could refer to\n       either \xe2\x80\x98Data.Text.find\xe2\x80\x99, imported from \xe2\x80\x98Data.Text\xe2\x80\x99\n           or \xe2\x80\x98Data.List.find\xe2\x80\x99,\n              imported from \xe2\x80\x98Data.List\xe2\x80\x99\n              (and originally defined in \xe2\x80\x98Data.Foldable\xe2\x80\x99)\n
Run Code Online (Sandbox Code Playgroud)\n

是这里使用的唯一解决方案Data.List.find (\\a -> a == 5) my_list,还是合格的进口?

\n

让我印象深刻的是

\n
    Ambiguous occurrence \xe2\x80\x98find\xe2\x80\x99\n    It could refer to\n       either \xe2\x80\x98Data.Text.find\xe2\x80\x99, imported from \xe2\x80\x98Data.Text\xe2\x80\x99\n           or \xe2\x80\x98Data.List.find\xe2\x80\x99,\n              imported from \xe2\x80\x98Data.List\xe2\x80\x99\n …
Run Code Online (Sandbox Code Playgroud)

haskell module

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

在 GHCI 中映射 IO [Int]

我想知道如何IO [Int]在 GHCI 中映射一个。

?: :{
?| th :: IO [Int]
?| th = pure [1, 2, 3, 4]
?| :}
?: th
[1,2,3,4]
?: :t th
th :: IO [Int]
?: map (+2) th
    • Couldn't match expected type ‘[b]’ with actual type ‘IO [Int]’
    • In the second argument of ‘map’, namely ‘th’
      In the expression: map (+ 2) th
Run Code Online (Sandbox Code Playgroud)

想要的结果:

?: res = map (+2) th -- <-- some working version of this
?: res …
Run Code Online (Sandbox Code Playgroud)

haskell ghci io-monad

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

在 Haskell 中合并多个案例

使用这样的case _ of语法:

fun a b c =
    case (a, b, c) of
        (Just True, Just _, _) -> foo
        (Just True, _, Just _) -> foo
        _                      -> bar
Run Code Online (Sandbox Code Playgroud)

我可以合并前两个条件并避免重复foo吗?

或者,有没有其他(更干净的)方式来表达我想运行foo当且仅当aisJust Truebor or cnot Nothing

haskell pattern-matching

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

仅在位置 0 处保留具有唯一值的子数组

从 Numpy nd 数组开始:

>>> arr
[
    [
        [10, 4, 5, 6, 7],
        [11, 1, 2, 3, 4],
        [11, 5, 6, 7, 8]
    ],
    [
        [12, 4, 5, 6, 7],
        [12, 1, 2, 3, 4],
        [12, 5, 6, 7, 8]
    ],
    [
        [15, 4, 5, 6, 7],
        [15, 1, 2, 3, 4],
        [15, 5, 6, 7, 8]
    ],
    [
        [13, 4, 5, 6, 7],
        [13, 1, 2, 3, 4],
        [14, 5, 6, 7, 8]
    ],
    [
        [10, 4, 5, 6, …
Run Code Online (Sandbox Code Playgroud)

python arrays numpy array-broadcasting

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