gpt4 book ai didi

c# 从文本文件中计算相同的字符串

转载 作者:行者123 更新时间:2023-11-30 13:17:01 24 4
gpt4 key购买 nike

我有一个 foreach 语句,我在该语句中遍历文本文件中的几行,我在其中修剪并整理出我需要的行。我想做的是计算相同字符串出现的次数。我该怎么做?

这是我的代码。这是我卡住的第二个 if 语句:

        foreach (string line in lines.Where(l => l.Length >= 5))
{
string a = line.Remove(0, 11);

if ((a.Contains(mobName) && a.Contains("dies")))
{

mobDeathCount++;
}
if (a.Contains(mobName) && a.Contains("drops"))
{
string lastpart = a.Substring(a.LastIndexOf("drops"));
string modifiedLastpart = lastpart.Remove(0, 6);

}

下面是一些行的样子:

一袋硬币

一杯白兰地

一袋硬币

一袋硬币

导管护盾

一个破烂的卷轴

所以我想做的是计算一袋硬币有 3 行。但我需要让它成为一切,有一个巨大的下拉列表。所以不能添加所有 em,会花费太长时间

编辑

    private static void Main()
{
int mobDeathCount = 1;
int lootCheckCount = 1;

string[] lines =
System.IO.File.ReadAllLines(@"C:\Users\Michael\Documents\Electronic Arts\Dark Age of Camelot\chat.log");
Console.WriteLine(
"Enter which mob you want to see, remember to include the, for an example; The siog seeker, remember to start with a capital T");
string mobName = Console.ReadLine();


foreach (string line in lines.Where(l => l.Length >= 5))
{




string a = line.Remove(0, 11);

if ((a.Contains(mobName) && a.Contains("dies")))
{

mobDeathCount++;
}
if (a.Contains(mobName) && a.Contains("drops"))
{
string lastpart = a.Substring(a.LastIndexOf("drops"));
string modifiedLastpart = lastpart.Remove(0, 6);

var lineCountDict = modifiedLastpart.GroupBy(x => x).Where(x => x.Count() > 1).ToDictionary(x => x.Key, x => x.Count());
foreach (var val in lineCountDict)
{
Console.WriteLine(val.Key + " - " + val.Value);
}

换行;

[01:09:55] siog 搜寻者掉了一袋硬币。

[01:09:55] siog 搜寻者掉了一杯 siog 白兰地。

[01:09:55] siog 搜寻者死了!

[01:09:55] 你获得 3,687,564 经验值。(1,638,917 营地奖励)

[01:10:31] 你施放了次级分解爆发法术!

[01:10:31] 你击中了 siog 导引头造成 424 (+18) 点伤害!

[01:10:31] siog 搜寻者掉了一袋硬币。

[01:10:31] 你捡起 18 个银币和 88 个铜币。

[01:10:31] siog 搜寻者死亡

最佳答案

您可以使用 LINQ 获取重复行数。这将创建一个字典,其中包含作为 key 的字符串以及作为 value 的字符串出现的次数。

var lineCountDict = lines.GroupBy(x => x).ToDictionary(x => x.Key, x => x.Count());

要读出值,只需遍历字典 所以,使用您的示例列表

List<String> lines = new List<string>()
{
"a bag of coins",
"a siog brandy",
"a bag of coins",
"a bag of coins",
"the Cath Shield",
"a tattered scroll"
};

var lineCountDict = lines.GroupBy(x => x).ToDictionary(x => x.Key, x => x.Count());

foreach (var val in lineCountDict)
{
Console.WriteLine(val.Key + " - " + val.Value);
}

这将输出每个字符串及其出现的次数,包括只出现一次的字符串。如果您只想要那些重复的,您可以通过添加 Where 子句来修改 LINQ 查询

var lineCountDict = lines.GroupBy(x => x).Where(x => x.Count() > 1).ToDictionary(x => x.Key, x => x.Count());

字典将只有您示例中列表中的一个项目(一袋硬币),键将是一袋硬币,值将是是 3 因为它出现了 3 次。

根据评论更新

这应该适用于您的情况

List<string> modifiedList = new List<string>();
int numberOfDrops = 0;

foreach (string line in lines.Where(l => l.Length >= 5))
{
string ad = line.Remove(0, 11);

if ((ad.Contains(mobName) && ad.Contains("dies")))
{
mobDeathCount++;
}
if (ad.Contains(mobName) && ad.Contains("drops"))
{
string lastpart = ad.Substring(ad.LastIndexOf("drops"));
string modifiedLastpart = lastpart.Remove(0, 6);
modifiedList.Add(modifiedLastpart);
numberOfDrops++;
}

}

double deathDropRatio = (double)mobDeathCount / (double)numberOfDrops;

var lineCountDict = modifiedList.GroupBy(x => x).Where(x => x.Count() > 1).ToDictionary(x => x.Key, x => x.Count());

foreach (var val in lineCountDict)
{
Console.WriteLine(val.Key + " - " + val.Value);
}

关于c# 从文本文件中计算相同的字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17351234/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com