简体   繁体   English

在List中查找项目是否在C#中多次出现

[英]Locating items in List whether they occur more than once in C#

In addition to the question: Get List<> element position in c# using LINQ 除了问题: 使用LINQ在c#中获取List <>元素位置

First of all, let me explaing why I do what I do :) 首先,让我解释一下为什么我会做我的事情:)

I'm trying to encrypt a string into a one-line code which holds how many times a letter is used in the string and also with its positions. 我正在尝试将字符串加密为单行代码,该代码包含在字符串中使用字母的次数及其位置。 By this method, I'll be able to decrypt the code and get(reproduce) the full text again. 通过这种方法,我将能够解密代码并再次获取(再现)全文。 Let's say myString is "How are you today". 假设myString是“您今天好吗”。 I'll encrypt it like o3[1,9,13]," "3[3,7,11],a2[4,15],y2[9,17],H1[0],w1[2],r1[5],e1[6],u1[10],t1[12],d1[14],?[18] 我将像o3 [1,9,13],“” 3 [3,7,11],a2 [4,15],y2 [9,17],H1 [0],w1 [2]一样对其进行加密, R1 [5],E1 [6],U1 [10],T1 [12],D1 [14],?[18]

I know it looks odd but think about working on bigger strings like e-books. 我知道这看起来很奇怪,但请考虑使用更大的字符串,例如电子书。 This could handle all the text in one or two lines. 这样可以在一两行中处理所有文本。

The encryption is not about the security, it's just about holding big datas in smaller spaces. 加密与安全性无关,而只是在较小的空间中保存大数据。

In my code, I can convert the string into list, count how many times a letter is used but I can't define the positions of the letters when they occur more than once. 在我的代码中,我可以将字符串转换为列表,计算字母的使用次数,但是当字母出现多次时,我无法定义字母的位置。

private void btnKoda_Click(object sender, EventArgs e)
    {
        var yazi = txtYazi.Text;
        List<char> liste = yazi.ToList();
        List<string> tut = new List<string>();

        foreach (char harf in liste)
        {
            for (int i = 0; i < liste.Count; i++)
            {
                char ekle = liste[i];
                tut.Add(ekle.ToString());
            }

            foreach (var karakter in tut)
            {
                txtKod.Text += karakter;
            }

            // holds statics
            var istatistik =
                from c in tut
                group c by c into g
                select new { g.Key, say = g.Count() };
            var enCok =
                from giris in istatistik
                orderby giris.say descending
                select giris;
            foreach (var giris in enCok)
            {
                txtHarfler.Text += string.Format("{0}: {1}\r\n", giris.Key, giris.say);
            }

            break;

        }

Not sure what's going on in your code, but here's how I would do it: 不知道您的代码中发生了什么,但是这是我的处理方式:

    private void button1_Click(object sender, EventArgs e)
    {
        Dictionary<Char, Encoding> dct = new Dictionary<char, Encoding>();

        string data = "How are you today";
        for(int i = 0; i < data.Length; i++)
        {
            Char C = data[i];
            if (!dct.ContainsKey(C))
            {
                dct.Add(C, new Encoding(C));
            }
            dct[C].AddOccurence(i);
        }

        StringBuilder SB = new StringBuilder();
        foreach(Encoding enc in dct.Values)
        {
            if (SB.Length == 0)
            {
                SB.Append(enc.ToString());
            }
            else
            {
                SB.Append("," + enc.ToString());
            }
        }

        Console.WriteLine(SB.ToString());
    }

Here's the Encoding class: 这是Encoding类:

    public class Encoding
    {

        private Char _C;
        private List<int> _Positions;

        private Encoding() {}

        public Encoding(Char C)
        {
            this._C = C;
            this._Positions = new List<int>();
        }

        public Char Character
        {
            get
            {
                return _C;
            }
        }

        public int Count
        {
            get
            {
                return _Positions.Count;
            }
        }

        public int[] Occurences
        {
            get
            {
                return _Positions.ToArray();
            }
        }

        public override string ToString()
        {
            string[] values = Array.ConvertAll(this.Occurences.ToArray(), x => x.ToString());
            return this.Character.ToString() + this.Count.ToString() + "[" + String.Join(",", values) + "]";
        }

        public void AddOccurence(int position)
        {
            this._Positions.Add(position);
        }

    }

Original String: 原始字串:

How are you today

Output: 输出:

H1[0],o3[1,9,13],w1[2], 3[3,7,11],a2[4,15],r1[5],e1[6],y2[8,16],u1[10],t1[12],d1[14]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM