对于可变类型,值和引用类型之间的行为差异很明显:
// Mutable value type
PointMutStruct pms1 = new PointMutStruct(1, 2);
PointMutStruct pms2 = pms1;
// pms1 == (1, 2); pms2 == (1, 2);
pms2.X = 3;
MutateState(pms1); // Changes the X property to 4.
// pms1 == (1, 2); pms2 == (3, 2);
// Mutable reference type
PointMutClass pmc1 = new PointMutClass(1, 2);
PointMutClass pmc2 = pmc1;
// pmc1 == (1, 2); pmc2 == (1, 2);
pmc2.X = 3;
MutateState(pmc1); // Changes the X property to 4.
// pmc1 …Run Code Online (Sandbox Code Playgroud) 我正在绘制具有类别和子类别的数据(请参阅下面的示例数据),并且我想要嵌套显示这些数据(此示例是在Excel中创建的):

我在R中提出的最好的方法是创建一个具有所需名称的新列,如下所示:
df <- data.frame(main.cat = c("A", "A", "B", "B", "B", "C"),
second.cat = c("a1", "a2", "b1", "b2", "b3", "c1"),
value = c(2, 3, 4, 2.5, 1.5, 2.3))
df$x.labels <- paste(df$second.cat, df$main.cat, sep = "\n")
ggplot(data = df, aes(x = x.labels, y = value)) + geom_point()
Run Code Online (Sandbox Code Playgroud)
这至少保留了两个级别的类别,但重复了所有主要类别标签:
有没有人知道更好的东西,看起来更像Excel的输出?
我想找到的不仅是应用于列表的函数的最大值(我将使用List.maxBy),而且还要找到列表中出现的值.这感觉就像一个相当普遍的操作,并且考虑到F#库的丰富性,一般情况下我发现它实际上已经可用并不会感到惊讶,但如果是的话我似乎无法找到它!
为了举例说明,我希望能够映射列表domain和函数f
let domain = [0 .. 5]
let f x = -x * (x - 2)
Run Code Online (Sandbox Code Playgroud)
to (1, 1)(因为应用于列表的其他元素的函数小于1).
我第一次尝试这个:
let findMaximum domain f =
let candidates = [ for x in domain do
yield x, f x ]
let rec findMaximumHelper domain f currentMax =
match domain with
| [] -> currentMax
| head::tail ->
let cand = f head
match currentMax with
| None ->
let newMax = Some(head, cand)
findMaximumHelper tail f …Run Code Online (Sandbox Code Playgroud) 我有一个entities包含name和entity_type列的表,并且想要查找多行测试这两个列的对。在普通的 SQL 中,我可以这样做:
SELECT *
FROM entities
WHERE (name, entity_type) IN (('abc', 'type1'), ('def', 'type2'))
Run Code Online (Sandbox Code Playgroud)
在 SQLAlchemy ORM 中,过滤 WHERE IN 单个列表很简单,例如:
session.query(Entity).filter(User.name.in_(['abc', 'def']))
Run Code Online (Sandbox Code Playgroud)
是否有与顶级 SQL 表达式等效的高效 SQLAlchemy?我不认为它特别相关,但就记录而言,我有兴趣针对 PostgreSQL 运行它。