有办法在C#中拥有一组<char, int>对,这两者都可以有重复的值吗?

huangapple go评论78阅读模式
英文:

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&gt;>" 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&lt;CharCount&gt;()

答案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 = &quot;aabaccbb&quot;;
        Dictionary&lt;int, MyChar&gt; pairs = new Dictionary&lt;int, MyChar&gt;();

        var chars = str.ToArray();
        int charNo = 0;
        for(int i = 0; i &lt; chars.Length; i++)
        {
            int j = 1;
            while (i + j &lt; chars.Length &amp;&amp; chars[i + j] == chars[i])
                j++;

            pairs[charNo] = new MyChar { Char = chars[i], Num = j }; 
            i += j - 1;
            charNo++;
        }

答案3

得分: 0

使用一个扩展方法来计算IEnumerable&lt;T&gt;中项目的运行次数,您只需将其转换为数组,即可获得一个有序的项目和计数的集合:

var s = "aabaccbb";
var ans = s.CountRuns()
           .ToArray();

这是该扩展方法的定义:

public static class IEnumerableExt {
    public static IEnumerable&lt;KeyValuePair&lt;T, int&gt;&gt; CountRuns&lt;T&gt;(this IEnumerable&lt;T&gt; items, IEqualityComparer&lt;T&gt; cmp = null) {
        cmp = cmp ?? EqualityComparer&lt;T&gt;.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&lt;T, int&gt;(key, count);
                        key = itemsEnum.Current;
                        count = 1;
                    }
                }
                yield return new KeyValuePair&lt;T, int&gt;(key, count);
            }
        }
    }
}
英文:

Using an extension method that counts runs of items from an IEnumerable&lt;T&gt;, you can just convert to an array to get an ordered collection of items and counts:

var s = &quot;aabaccbb&quot;;
var ans = s.CountRuns()
           .ToArray();

Here is the extension:

public static class IEnumerableExt {
    public static IEnumerable&lt;KeyValuePair&lt;T, int&gt;&gt; CountRuns&lt;T&gt;(this IEnumerable&lt;T&gt; items, IEqualityComparer&lt;T&gt; cmp = null) {
        cmp = cmp ?? EqualityComparer&lt;T&gt;.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&lt;T, int&gt;(key, count);
                        key = itemsEnum.Current;
                        count = 1;
                    }
                }
                yield return new KeyValuePair&lt;T, int&gt;(key, count);
            }
        }
    }
}

huangapple
  • 本文由 发表于 2023年6月2日 05:51:42
  • 转载请务必保留本文链接:https://go.coder-hub.com/76385952.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定