根据数量将列表中的不同元素配对

时间:2017-12-16 19:39:26

标签: c#

//Streamwriter object used to create the text file
        StreamWriter sw = new StreamWriter(@"C:\Users\Tarek_Akkawi\Desktop\SynmaticsInputFile.txt");

        //WriteLine used instead of Write in order to introduce a new line for each person
        sw.WriteLine("lucas 8 4 5");
        sw.WriteLine("lucas 8");
        sw.WriteLine("Alex 1 2 3");
        sw.WriteLine("Alex 4 22 4");
        sw.WriteLine("nia 22 3");
        sw.WriteLine("nia 2 3");


        sw.Close();




        OuputLogic opl = new OuputLogic();


public class OuputLogic
{

    public OuputLogic()
    {
        //using constructor to initialize the new file
        Pair();
    }

    //Using streamread class to read the file
    StreamReader sr = new StreamReader(@"C:\Users\Tarek_Akkawi\Desktop\SynmaticsInputFile.txt");

    //Method to read text file
    public string ReadFile()
    {
        string x = sr.ReadToEnd();
        return x;

    }


    //Method To manipulate TExt file
    public void Pair()
    {

        List<string> getdata = ReadFile().Trim().Split().ToList();


        var tuples = getdata.Where((x, i) => i % 2 == 0).Zip(getdata.Where((x, i) => i % 2 == 1), (a, b) => new Tuple<string, string>(a, b)).ToArray();

所以我试图根据每个名字的数字数量在我的列表中配对我的项目。所以这个想法是我想把卢卡斯与8 4 5然后卢卡斯与8等等配对。 基于我已经完成的研究,我最接近实现的是通过上面的元组,但它基于模数的结果进行配对。 我正在寻找能够将每个名字配对的东西,包括任意数字的数字。

0 个答案:

没有答案