locked
How can i move from a list filter out lists that are count is 1 ? RRS feed

  • Question

  • In the top

    [System.Serializable]
        public class PosDisPair 
        {
            public float Distance;
            public Vector3 Pos;
        }

    Then

    private List<PosDisPair> pairList = new List<PosDisPair>();

    And

    private void FindDistances()
        {
            pairList = new List<PosDisPair>();
    
            for (int i = 0; i < objects.Count; i++)
            {
                PosDisPair pairToAdd = new PosDisPair();
                pairToAdd.Distance = Vector3.Distance(EndStartPoints[0].transform.position, objects[i].transform.position);
                pairToAdd.Pos = objects[i].transform.position;
                pairList.Add(pairToAdd);
            }
    
            pairList.Sort(delegate (PosDisPair a, PosDisPair b)
            {
                return (a.Distance.CompareTo(b.Distance));
            });
    
            Test();
        }

    Then in Test

    private void Test()
        {
            List<float> samedistances = new List<float>();
    
            var groupedList = pairList
        .GroupBy(u => u.Distance)
        .Select(grp => grp.ToList())
        .ToList();
    
            for (int i = 0; i < groupedList.Count; i++)
            {
              if (groupedList[i].Count == 1)
                {
                    groupedList.Remove(groupedList[i]);
                }
            }
        }

    Now groupedList is List<List<PosDisPair>> and have 72 grouped lists.

    Now i want to remove filter out from groupedList all the Lists that they count is 1

    But after the loop in Test i still see many lists with count 1.

    How can i remove all the lists of count 1 ?

    Thursday, August 17, 2017 6:31 PM

Answers

  • Hello Chocolade1972,

    please pay attention the following statement.

    groupedList.Remove(groupedList[i]);

    when you execute the above statement, it will also influence the groupedList.Count , the executing times of ‘for’ statement also has affected . This is the reason why many list with count 1 after loop,you could modify the code quickly as below.

    for (int i = 0; i < groupedList.Count; i++)
                {
                    if (groupedList[i].Count == 1)
                    {
                        groupedList.Remove(groupedList[i]);
                       i--;
                    }
                }

    Best regards,

    feih_7


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Edited by Fei Hu Friday, August 18, 2017 2:29 AM
    • Marked as answer by Chocolade1972 Friday, August 18, 2017 12:21 PM
    Friday, August 18, 2017 2:28 AM

All replies

  • Hello Chocolade1972,

    please pay attention the following statement.

    groupedList.Remove(groupedList[i]);

    when you execute the above statement, it will also influence the groupedList.Count , the executing times of ‘for’ statement also has affected . This is the reason why many list with count 1 after loop,you could modify the code quickly as below.

    for (int i = 0; i < groupedList.Count; i++)
                {
                    if (groupedList[i].Count == 1)
                    {
                        groupedList.Remove(groupedList[i]);
                       i--;
                    }
                }

    Best regards,

    feih_7


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Edited by Fei Hu Friday, August 18, 2017 2:29 AM
    • Marked as answer by Chocolade1972 Friday, August 18, 2017 12:21 PM
    Friday, August 18, 2017 2:28 AM
  • Hi,

    do the loop in reverse direction when modifying the count of the list:

            for (int i = groupedList.Count - 1; i >= 0; i--)
            {
              if (groupedList[i].Count == 1)
                {
                    groupedList.Remove(groupedList[i]);
                }
            }

    Regards,

      Thorsten

    Friday, August 18, 2017 3:30 AM