可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
I'm using streamreader now to read a file of people names, it is a text file, of people first names, so there are obviously duplicates, and i want to be able to display how many people have the same now so for example:
josh
alex
josh
john
alex
I want it to say,
josh 2
alex 2
john 1
but I can't seem to find an easy way of doing this, what would be the easiest way about doing this,
回答1:
I'd say use a Dictionary<string, int>
.
Dictionary<string, int> firstNames = new Dictionary<string, int>();
foreach (string name in YourListWithNames)
{
if (!firstNames.ContainsKey(name))
firstNames.Add(name, 1);
else
firstNames[name] += 1;
}
Of course there are many different paths to a solution, but this is how I would tackle it. I haven't run this code yet, but this will help you I think.
回答2:
Try this with LINQ.
First read your text file to a List<string>
using this code:
const string f = "TextFile1.txt";
// 1
// Declare new List.
List<string> lines = new List<string>();
// 2
// Use using StreamReader for disposing.
using (StreamReader r = new StreamReader(f))
{
// 3
// Use while != null pattern for loop
string line;
while ((line = r.ReadLine()) != null)
{
// 4
// Insert logic here.
// ...
// "line" is a line in the file. Add it to our List.
lines.Add(line);
}
}
You need to define a class where you will have name and accordingly the count:
class PersonCount
{
public string Name { get; set; }
public int Count { get; set; }
}
And finally use this Lambda
expression to get desired List<string>
List<PersonCount> personCounts = lines.GroupBy(p => p).Select(g => new PersonCount() {Name = g.Key, Count = g.Count()}).ToList();
Now iterate through the list to get the names and the count of duplicates.
回答3:
Using an HashMap is a solution to your problem.
When you read a name, check if the key is already present, if so, update it (+1), if not add it to your Hash Map.
In the end, all you need to do is print the key-value pairs.
回答4:
Store all names in a Dictionary<string, int> names
.
Use something like this for each row:
var theName = reader.ReadLine();
names[theName] += 1;
(it should set the count to one if the item do not exist)
回答5:
foreach (var keyvalue in File.ReadAllLines(@"C:\....").GroupBy(x => x).Select(x => new { name = x.Key, count = x.Count() }))
{
Console.WriteLine(keyvalue.name + ": " + keyvalue.count);
}
回答6:
You could of course also do something like this (error checking omitted), using Linq:
var names = new List<string>(
File.ReadAllText(pathToFile).Split(
Environment.NewLine.ToCharArray(),
StringSplitOptions.RemoveEmptyEntries
));
var namesAndOccurrences =
from name in names.Distinct()
select name + " " + names.Count(n => n == name);
foreach (var name in namesAndOccurrences)
Console.WriteLine(name);
Depending on the size of the file, this might be desirable to get rid of the stream; however, this isn't to say that if the file was considerably large for memory that you should use ReadLine
.
回答7:
try this offline solution
StreamReader dr = new StreamReader(@"C:\txt.txt");
string str = dr.ReadToEnd();
string[] p = str.Split(new string[] { Environment.NewLine, " " }, StringSplitOptions.RemoveEmptyEntries);
Dictionary<string, int> count = new Dictionary<string, int>();
for (int i = 0; i < p.Length; i++)
{
try
{
count[p[i].Trim()] = count[p[i]] + 1;
}
catch
{
count.Add(p[i], 1);
}
}