- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
这就是我在 C# 中创建字典的方式。
Dictionary<string, int> d = new Dictionary<string, int>()
{
{"cheese", 2},
{"cakes", 1},
{"milk", 0},
{"humans", -1} // This one's for laughs
};
在 Python 中,如果你有这样的字典:
from collections import Counter
my_first_dict = {
"cheese": 1,
"cakes": 2,
"milk": 3,
}
my_second_dict = {
"cheese": 0,
"cakes": 1,
"milk": 4,
}
print Counter(my_first_dict) - Counter(my_second_dict)
>>> Counter({'cheese': 1, 'cakes': 1})
如您所见,Counter
在比较字典对象时非常有用。
C# 中是否有一个库可以让我做类似的事情,还是我必须从头开始编写代码?
最佳答案
只需几行代码,您就可以将两个字典连接在一起,然后根据给定的操作创建一个新字典:
Dictionary<string, int> d1 = new Dictionary<string, int>();
Dictionary<string, int> d2 = new Dictionary<string, int>();
var difference = d1.Join(d2, pair => pair.Key, pair => pair.Key, (a, b) => new
{
Key = a.Key,
Value = a.Value - b.Value,
})
.Where(pair => pair.Value > 0)
.ToDictionary(pair => pair.Key, pair => pair.Value);
您没有显示任何系统类来包装字典并为它们提供 -
运算符,但是如果您想要足够简单,您可以创建自己的系统类:
public class Counter<T> : IEnumerable<KeyValuePair<T, int>>
{
private IEnumerable<KeyValuePair<T, int>> sequence;
public Counter(IEnumerable<KeyValuePair<T, int>> sequence)
{
this.sequence = sequence;
}
public static Counter<T> operator -(Counter<T> first, Counter<T> second)
{
return new Counter<T>(first.Join(second
, pair => pair.Key, pair => pair.Key, (a, b) =>
new KeyValuePair<T, int>(a.Key, a.Value - b.Value))
.Where(pair => pair.Value > 0));
}
public IEnumerator<KeyValuePair<T, int>> GetEnumerator()
{
return sequence.GetEnumerator();
}
IEnumerator IEnumerable.GetEnumerator()
{
return GetEnumerator();
}
}
关于c# - 像 Python 的 collections.Counter 库这样的 C# 库 -> 在 C# 中获取两个字典对象之间的值差异,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19033870/
哪个更快? Counter()+=Counter 或 Counter.update(Counter)? 为什么一个比另一个更快? 我尝试了一些简单的分析,但我认为这不足以最终证明 Counter+=C
这个问题在这里已经有了答案: ++someVariable vs. someVariable++ in JavaScript (7 个答案) 关闭 7 年前。 var counter = 0; va
下面是我正在使用的代码。如果我按 addQuanity m_label 设置显示一个而不是两个。如果我再次按 addWuantity,m_label 显示 2。按 minusQuantity 将 m_
这个问题已经有答案了: Does Java evaluate remaining conditions after boolean result is known? (7 个回答) 已关闭 6 年前。
因此,当我将计数器(from collections import Counter)打印到一个文件时,我总是得到它的文字 Counter ({'Foo': 12}) 有没有办法让计数器不那么字面地写出
我正在使用 CSS2.1 计数器将数字应用于棋盘上的人,以实现棋盘游戏,其棋盘图使用 HTML 和 CSS,方法如下: .ply {counter-increment:main;} .move:be
这个问题在这里已经有了答案: 关闭 11 年前。 Possible Duplicate: Is there a performance difference between i++ and ++i
我在尝试编译 Arduino 草图时遇到此错误。我看不出它认为我试图在没有参数的情况下调用 Counter::Counter 的地方。这是怎么回事? sketch/periodic_effect.cp
调用Get-Counter时使用-ComputerName参数和使用-Counter参数中的路径有区别吗? Get-Counter -Counter "\Memory\Available MB
姓名 Counter在 collections 中都定义了(作为一个类(class))和在 typing (作为通用类型名称)。不幸的是,它们略有不同。处理这个问题的推荐方法是什么? 相同点和不同点:
此代码不会给出任何失败,但如果您使用 counter++,则第一次迭代会失败。 parameters="one two three" counter=0 for option in $param
powershell 中的 get-counter/export-counter cmdlet 似乎以美国格式返回日期,这在这种情况下是相当不受欢迎的。我浏览了两个 get-help -full 页面
我有 2 个计数器(来自集合的计数器),我想将一个附加到另一个,而第一个计数器的重叠键将被忽略。喜欢 dic.update (python 词典更新) 例如: from collections imp
我想在我的项目中为 Provider ( ChangeNotifierProvider ) 创建一个单元测试,我的单元测试、小部件测试和集成测试成功通过 ✔️,所以现在我尝试(努力尝试🥵...)创建
我知道以下代码的复杂度为 O(log(n)): while (n>1) { counter++; n/=2; } 我知道在这里,n 在每次迭代中被分成两半,这意味着如果 n 是 100
Counter.getName() 方法与 Counter.getDisplayName() 方法有什么区别。我没有从文档中看到太多信息 http://hadoop.apache.org/docs/r
我有一个 python 文件,用于在 Hadoop(版本 2.6.0)上使用 mrjob 来计算二元语法,但我没有得到我希望的输出,而且我在破译终端中的输出时遇到了问题我哪里出错了。 我的代码: re
我看到带有错误消息的事件 ID 2001: It has taken too long to refresh the W3SVC counters , the stale counters are b
我对 React 完全陌生,我正在 YouTube 上学习教程(使用 MOSH 编程),但我遇到了这个错误,在找到类似问题后无法解决。 index.js import React from 'reac
我正在运行一个 hadoop 作业(来自 oozie),它有几个计数器和多输出。 我收到如下错误:org.apache.hadoop.mapreduce.counters.LimitExceededE
我是一名优秀的程序员,十分优秀!