英文:
Is there a way to have a collection of <char, int> pairs which both can have duplicate values in C#?
问题
我想找到给定字符串中一行中每个字符的计数。
例如,“aabaccbb”有两个 'a',一个 'b',一个 'a',两个 'c',和两个 'b'。
我想要一个 <char, int> 对的集合,对我来说顺序很重要。
例如,上面的示例如下所示:
{ {'a', 2}, {'b', 1}, {'a', 1}, {'c', 2}, {'b', 2} }
- "Dictionary<char, int>" 不能有重复的键(char)。
 - "List< KeyValuePair<char, int> >" 不错,但键和值都是只读的,我需要修改它们。
 - "List<char, List
>" 也不适用,因为对我来说顺序很重要。  
那么我该怎么办?
英文:
I wanna find the count of each character in a row in a given string.
For example "aabaccbb" has two 'a', one 'b', one 'a', two 'c', and two 'b'.
I wanna have a collection of <char,int> pairs and the order is important for me.
For example above like this:
{ {'a', 2}, {'b', 1}, {'a', 1}, {'c', 2}, {'b', 2} }
- "Dictionary<char, int>" can't have duplicate keys (char).
 - "List< <KeyValuePair<char, int> >" is good but the key and value are read-only and I need to modify them.
 - "List<char, List<int>>" isn't suitable too because the order is important for me.
 
So what can I do?
答案1
得分: 0
我认为你只是想创建一个类来保存这些值,然后创建一个该类型的列表
public class CharCount
{
    public char Char { get; set; }
    public int Count { get; set; }
}
然后只需创建你的该类型的列表
new List<CharCount>()
英文:
I think you just want to make a class to hold the values for you and then make a list of that type
public class CharCount
{
	public char Char { get; set; }
	public int Count { get; set; }
}
then just create your list of that type
new List<CharCount>()
答案2
得分: 0
你可以创建一个类
public class MyChar
{
    public char Char { get; set; }
    public int Num { get; set; }
}
并像这样使用它。
string str = "aabaccbb";
Dictionary<int, MyChar> pairs = new Dictionary<int, MyChar>();
var chars = str.ToArray();
int charNo = 0;
for (int i = 0; i < chars.Length; i++)
{
    int j = 1;
    while (i + j < chars.Length && chars[i + j] == chars[i])
        j++;
    pairs[charNo] = new MyChar { Char = chars[i], Num = j };
    i += j - 1;
    charNo++;
}
英文:
You can create a class
public class MyChar
{
    public char Char { get; set; }
    public int Num { get; set; }
}
And use it like this.
        string str = "aabaccbb";
        Dictionary<int, MyChar> pairs = new Dictionary<int, MyChar>();
        var chars = str.ToArray();
        int charNo = 0;
        for(int i = 0; i < chars.Length; i++)
        {
            int j = 1;
            while (i + j < chars.Length && chars[i + j] == chars[i])
                j++;
            pairs[charNo] = new MyChar { Char = chars[i], Num = j }; 
            i += j - 1;
            charNo++;
        }
答案3
得分: 0
使用一个扩展方法来计算IEnumerable<T>中项目的运行次数,您只需将其转换为数组,即可获得一个有序的项目和计数的集合:
var s = "aabaccbb";
var ans = s.CountRuns()
           .ToArray();
这是该扩展方法的定义:
public static class IEnumerableExt {
    public static IEnumerable<KeyValuePair<T, int>> CountRuns<T>(this IEnumerable<T> items, IEqualityComparer<T> cmp = null) {
        cmp = cmp ?? EqualityComparer<T>.Default;
        using (var itemsEnum = items.GetEnumerator()) {
            if (itemsEnum.MoveNext()) {
                var key = itemsEnum.Current;
                var count = 1;
                while (itemsEnum.MoveNext()) {
                    if (cmp.Equals(key, itemsEnum.Current))
                        ++count;
                    else {
                        yield return new KeyValuePair<T, int>(key, count);
                        key = itemsEnum.Current;
                        count = 1;
                    }
                }
                yield return new KeyValuePair<T, int>(key, count);
            }
        }
    }
}
英文:
Using an extension method that counts runs of items from an IEnumerable<T>, you can just convert to an array to get an ordered collection of items and counts:
var s = "aabaccbb";
var ans = s.CountRuns()
           .ToArray();
Here is the extension:
public static class IEnumerableExt {
    public static IEnumerable<KeyValuePair<T, int>> CountRuns<T>(this IEnumerable<T> items, IEqualityComparer<T> cmp = null) {
        cmp = cmp ?? EqualityComparer<T>.Default;
        using (var itemsEnum = items.GetEnumerator()) {
            if (itemsEnum.MoveNext()) {
                var key = itemsEnum.Current;
                var count = 1;
                while (itemsEnum.MoveNext()) {
                    if (cmp.Equals(key, itemsEnum.Current))
                        ++count;
                    else {
                        yield return new KeyValuePair<T, int>(key, count);
                        key = itemsEnum.Current;
                        count = 1;
                    }
                }
                yield return new KeyValuePair<T, int>(key, count);
            }
        }
    }
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。


评论