我正在尝试切换到只使用python。我在C中使用得相当广泛的是LINQ。在这个练习中,目标是获取一组键-值对,键是每个月的,值是该月消息数的一个计数,如何使用python执行类似的操作,或者可能有什么更好的方法可以做到这一点?在
class MainClass
{
public static void Main (string[] args)
{
string[] months = { "jan", "feb", "mar", "apr", "may", "jun", "jul", "aug", "sep", "oct", "nov", "dec" };
var log = LineReader ();
Dictionary<string, int> cumulativeMonths = new Dictionary<string, int> ();
months.ToList ()
.ForEach (f => {
cumulativeMonths.Add(f, log.GroupBy(g => g.Split(' ').First().ToLower())
.Where(w => w.Key == f).ToList().Count());
});
}
public static IEnumerable<string> LineReader()
{
Console.WriteLine ("Hello World!");
using (StreamReader sr = new StreamReader (File.OpenRead ("/var/log/messages"))) {
while (!sr.EndOfStream) {
yield return sr.ReadLine ();
}
}
}
}
测试输入:
^{pr2}$测试输出将是一个字典: {一月:64562,二月:38762}。。。。在
这比您所做的要简单,而且在Python中也非常简单:
with
类似于C的using
,当文件超出作用域时将关闭它。python文件对象可以用作迭代器。它将一次读取并返回一行,直到到达EOF为止。(它实际上读取的内容不止一行,请参见documentation)。在或者,正如m.wasowski所指出的,您可以将
collections.Counter
类用于这种类型的任务,以使事情变得更简单、更快。在您可以使用collections.Counterdict:
很抱歉有任何错误,这是从手机写的:)
是的,这正是我自己想出的,我想我想知道是否有一种更优雅(一行式)的方法来解决这个问题:
相关问题 更多 >
编程相关推荐