考虑以下架构:
defmodule EctoBug.Post do
use Ecto.Schema
import Ecto.Changeset
schema "posts" do
field :title, :string, default: "test"
timestamps()
end
def changeset(post, attrs) do
post
|> cast(attrs, [:title])
|> validate_required([:title])
end
end
Run Code Online (Sandbox Code Playgroud)
如果我做
changeset = EctoBug.Post.changeset(%EctoBug.Post{}, %{title: "test"})
Run Code Online (Sandbox Code Playgroud)
title字段不存在于changes:
#Ecto.Changeset<action: nil, changes: %{}, errors: [], data: #EctoBug.Post<>, valid?: true>
Run Code Online (Sandbox Code Playgroud)
我找不到任何关于这种行为的信息。
这是一个错误吗?
是否可以使用json.Decoder解码顶级JSON数组?
或者阅读整个JSON和json.Unmarshall是这种情况下唯一的方法吗?
我已经阅读了这个问题中接受的答案,并且无法弄清楚如何将它与顶级JSON数组一起使用
如何实现one?和none?在药剂的有效途径?
你觉得这个怎么样?
defmodule MyEnum do
def one?(enum, fun) do
Enum.count(enum, fun) == 1 && true || false
end
def none?(enum, fun) do
Enum.count(enum, fun) == 0 && true || false
end
end
Run Code Online (Sandbox Code Playgroud)
用法示例:
iex(6)> MyEnum.none?([0,0,0,0], fn(x) -> x == 1 end)
true
iex(7)> MyEnum.none?([0,0,1,0], fn(x) -> x == 1 end)
false
iex(8)> MyEnum.none?([0,1,1,0], fn(x) -> x == 1 end)
false
iex(9)> MyEnum.one?([0,1,1,0], fn(x) -> x == 1 end)
false
iex(10)> MyEnum.one?([0,1,0,0], fn(x) -> x == 1 …Run Code Online (Sandbox Code Playgroud) 我有以下数据框:
import pandas as pd
data = [['10', '20'], ['10', '15'], ['15', '14']]
df = pd.DataFrame(data, columns = ['dt', 'ct'])
df.groupby('dt')['dt'].count()
Run Code Online (Sandbox Code Playgroud)
退货
dt
10 2
15 1
Run Code Online (Sandbox Code Playgroud)
和
df.groupby('ct')['ct'].count()
Run Code Online (Sandbox Code Playgroud)
退货
ct
14 1
15 1
20 1
Run Code Online (Sandbox Code Playgroud)
然后当我合并结果时
df.groupby('ct')['ct'].count() + df.groupby('dt')['dt'].count()
Run Code Online (Sandbox Code Playgroud)
它返回
10 NaN
14 NaN
15 2.0
20 NaN
Run Code Online (Sandbox Code Playgroud)
但我想得到以下内容:
10 2
14 1
15 2.0
20 1
Run Code Online (Sandbox Code Playgroud) 考虑以下代码:
public class StreamDemo {
public static void main(String[] args) {
StreamObject obj = new StreamObject();
obj.setName("mystream");
List<Integer> list = new ArrayList<>(Arrays.asList(1, 2, 3, 4, 5));
list.parallelStream().forEach(l -> {
obj.setId(l);
System.out.println(obj + Thread.currentThread().getName());
});
}
static public class StreamObject {
private String name;
private Integer id;
// getters, setters, toString()
}
}
Run Code Online (Sandbox Code Playgroud)
当使用 java 11 编译并运行时,它返回以下内容:
StreamObject{name='mystream', id=4}ForkJoinPool.commonPool-worker-23
StreamObject{name='mystream', id=4}main
StreamObject{name='mystream', id=4}ForkJoinPool.commonPool-worker-9
StreamObject{name='mystream', id=4}ForkJoinPool.commonPool-worker-5
StreamObject{name='mystream', id=4}ForkJoinPool.commonPool-worker-19
Run Code Online (Sandbox Code Playgroud)
但对于 java 1.8,它返回不同的结果:
StreamObject{name='mystream', id=3}main
StreamObject{name='mystream', id=5}ForkJoinPool.commonPool-worker-2
StreamObject{name='mystream', id=2}ForkJoinPool.commonPool-worker-9
StreamObject{name='mystream', id=1}ForkJoinPool.commonPool-worker-11
StreamObject{name='mystream', id=4}ForkJoinPool.commonPool-worker-4
Run Code Online (Sandbox Code Playgroud)
为什么结果不同?