compare two items in list, and then split into smaller list at index












0














So i have a list of locations. I need to split the list if the distance between each location is greater than say 30.



I can loop through the list and get the distance between each location, i am just not sure what the best approach is to split the list, i have read answers that break the list into chunks with a set size, but in my case the size could be variable depending on the distance between locations.



This could be really simple and i just cant see it. What i have so far is below, the code is pretty straightforward in comparing the two items, its purely splitting the list i am stuck at. Currently my code would not include all the items from the original list, it would exclude the items before the first GetRange.



var unkownSegments = grouped.Where(x => x.ActivityType == null);

foreach (var group in unkownSegments)
{
var tempLists = new List<List<LocationResult>>();
for (int i = 0; i < group.Items.Count - 1; i++)
{
var point1 = group.Items[i];
var point2 = group.Items[i + 1];

var sCoord = new GeoCoordinate(point1.Lat, point1.Long);
var eCoord = new GeoCoordinate(point2.Lat, point2.Long);

var distance = sCoord.GetDistanceTo(eCoord);

if(distance > 30)
{
var tempList = group.Items.GetRange(i, group.Items.Count - i);
tempLists.Add(tempList);
}
}
}


Thank you for any help or suggestions.










share|improve this question





























    0














    So i have a list of locations. I need to split the list if the distance between each location is greater than say 30.



    I can loop through the list and get the distance between each location, i am just not sure what the best approach is to split the list, i have read answers that break the list into chunks with a set size, but in my case the size could be variable depending on the distance between locations.



    This could be really simple and i just cant see it. What i have so far is below, the code is pretty straightforward in comparing the two items, its purely splitting the list i am stuck at. Currently my code would not include all the items from the original list, it would exclude the items before the first GetRange.



    var unkownSegments = grouped.Where(x => x.ActivityType == null);

    foreach (var group in unkownSegments)
    {
    var tempLists = new List<List<LocationResult>>();
    for (int i = 0; i < group.Items.Count - 1; i++)
    {
    var point1 = group.Items[i];
    var point2 = group.Items[i + 1];

    var sCoord = new GeoCoordinate(point1.Lat, point1.Long);
    var eCoord = new GeoCoordinate(point2.Lat, point2.Long);

    var distance = sCoord.GetDistanceTo(eCoord);

    if(distance > 30)
    {
    var tempList = group.Items.GetRange(i, group.Items.Count - i);
    tempLists.Add(tempList);
    }
    }
    }


    Thank you for any help or suggestions.










    share|improve this question



























      0












      0








      0







      So i have a list of locations. I need to split the list if the distance between each location is greater than say 30.



      I can loop through the list and get the distance between each location, i am just not sure what the best approach is to split the list, i have read answers that break the list into chunks with a set size, but in my case the size could be variable depending on the distance between locations.



      This could be really simple and i just cant see it. What i have so far is below, the code is pretty straightforward in comparing the two items, its purely splitting the list i am stuck at. Currently my code would not include all the items from the original list, it would exclude the items before the first GetRange.



      var unkownSegments = grouped.Where(x => x.ActivityType == null);

      foreach (var group in unkownSegments)
      {
      var tempLists = new List<List<LocationResult>>();
      for (int i = 0; i < group.Items.Count - 1; i++)
      {
      var point1 = group.Items[i];
      var point2 = group.Items[i + 1];

      var sCoord = new GeoCoordinate(point1.Lat, point1.Long);
      var eCoord = new GeoCoordinate(point2.Lat, point2.Long);

      var distance = sCoord.GetDistanceTo(eCoord);

      if(distance > 30)
      {
      var tempList = group.Items.GetRange(i, group.Items.Count - i);
      tempLists.Add(tempList);
      }
      }
      }


      Thank you for any help or suggestions.










      share|improve this question















      So i have a list of locations. I need to split the list if the distance between each location is greater than say 30.



      I can loop through the list and get the distance between each location, i am just not sure what the best approach is to split the list, i have read answers that break the list into chunks with a set size, but in my case the size could be variable depending on the distance between locations.



      This could be really simple and i just cant see it. What i have so far is below, the code is pretty straightforward in comparing the two items, its purely splitting the list i am stuck at. Currently my code would not include all the items from the original list, it would exclude the items before the first GetRange.



      var unkownSegments = grouped.Where(x => x.ActivityType == null);

      foreach (var group in unkownSegments)
      {
      var tempLists = new List<List<LocationResult>>();
      for (int i = 0; i < group.Items.Count - 1; i++)
      {
      var point1 = group.Items[i];
      var point2 = group.Items[i + 1];

      var sCoord = new GeoCoordinate(point1.Lat, point1.Long);
      var eCoord = new GeoCoordinate(point2.Lat, point2.Long);

      var distance = sCoord.GetDistanceTo(eCoord);

      if(distance > 30)
      {
      var tempList = group.Items.GetRange(i, group.Items.Count - i);
      tempLists.Add(tempList);
      }
      }
      }


      Thank you for any help or suggestions.







      c#






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 23 at 0:09









      Ňuf

      4,84921222




      4,84921222










      asked Nov 22 at 22:18









      Tony_89

      383422




      383422
























          1 Answer
          1






          active

          oldest

          votes


















          1














          To create a range (using GetRange() method), you need to know where it begins and where it ends. If distance between Item[i] and Item[i+1] is greater then 30, you know the end, because that end is at index i. But you don't know the beginning (of course, you know it for the first range - it's 0), because beginning depends on the end of previous range. So you need to introduce new variable (it's called rangeStart in my example bellow), that will contain such information. It starts with value 0 (that's where first range always begins) and then update it's value whenever you add new range (next range will always start at index i+1).



          After the for loop finishes, some points will remain. So need to add them points as the last range. Whole method can then look like this:



          var unkownSegments = grouped.Where(x => x.ActivityType == null);

          foreach (var group in unkownSegments)
          {
          var tempLists = new List<List<LocationResult>>();

          //This variable keeps track of the beginning of the next range
          var rangeStart = 0;

          for (int i = 0; i < group.Items.Count - 1; i++)
          {
          var point1 = group.Items[i];
          var point2 = group.Items[i + 1];

          var sCoord = new GeoCoordinate(point1.Lat, point1.Long);
          var eCoord = new GeoCoordinate(point2.Lat, point2.Long);

          var distance = sCoord.GetDistanceTo(eCoord);

          if(distance > 30)
          {
          var tempList = group.Items.GetRange(rangeStart, i - rangeStart + 1);
          tempLists.Add(tempList);
          rangeStart = i + 1;//Next range will begin on the following item
          }
          }

          if (group.Items.Count - rangeStart > 0)
          {
          //Add all remainging (not added yet) points as the last range.
          var tempList = group.Items.GetRange(rangeStart, group.Items.Count - rangeStart);
          tempLists.Add(tempList);
          }
          }





          share|improve this answer





















          • Thank you, exactly what i was looking for.
            – Tony_89
            Nov 23 at 10:26











          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53438583%2fcompare-two-items-in-list-and-then-split-into-smaller-list-at-index%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          1














          To create a range (using GetRange() method), you need to know where it begins and where it ends. If distance between Item[i] and Item[i+1] is greater then 30, you know the end, because that end is at index i. But you don't know the beginning (of course, you know it for the first range - it's 0), because beginning depends on the end of previous range. So you need to introduce new variable (it's called rangeStart in my example bellow), that will contain such information. It starts with value 0 (that's where first range always begins) and then update it's value whenever you add new range (next range will always start at index i+1).



          After the for loop finishes, some points will remain. So need to add them points as the last range. Whole method can then look like this:



          var unkownSegments = grouped.Where(x => x.ActivityType == null);

          foreach (var group in unkownSegments)
          {
          var tempLists = new List<List<LocationResult>>();

          //This variable keeps track of the beginning of the next range
          var rangeStart = 0;

          for (int i = 0; i < group.Items.Count - 1; i++)
          {
          var point1 = group.Items[i];
          var point2 = group.Items[i + 1];

          var sCoord = new GeoCoordinate(point1.Lat, point1.Long);
          var eCoord = new GeoCoordinate(point2.Lat, point2.Long);

          var distance = sCoord.GetDistanceTo(eCoord);

          if(distance > 30)
          {
          var tempList = group.Items.GetRange(rangeStart, i - rangeStart + 1);
          tempLists.Add(tempList);
          rangeStart = i + 1;//Next range will begin on the following item
          }
          }

          if (group.Items.Count - rangeStart > 0)
          {
          //Add all remainging (not added yet) points as the last range.
          var tempList = group.Items.GetRange(rangeStart, group.Items.Count - rangeStart);
          tempLists.Add(tempList);
          }
          }





          share|improve this answer





















          • Thank you, exactly what i was looking for.
            – Tony_89
            Nov 23 at 10:26
















          1














          To create a range (using GetRange() method), you need to know where it begins and where it ends. If distance between Item[i] and Item[i+1] is greater then 30, you know the end, because that end is at index i. But you don't know the beginning (of course, you know it for the first range - it's 0), because beginning depends on the end of previous range. So you need to introduce new variable (it's called rangeStart in my example bellow), that will contain such information. It starts with value 0 (that's where first range always begins) and then update it's value whenever you add new range (next range will always start at index i+1).



          After the for loop finishes, some points will remain. So need to add them points as the last range. Whole method can then look like this:



          var unkownSegments = grouped.Where(x => x.ActivityType == null);

          foreach (var group in unkownSegments)
          {
          var tempLists = new List<List<LocationResult>>();

          //This variable keeps track of the beginning of the next range
          var rangeStart = 0;

          for (int i = 0; i < group.Items.Count - 1; i++)
          {
          var point1 = group.Items[i];
          var point2 = group.Items[i + 1];

          var sCoord = new GeoCoordinate(point1.Lat, point1.Long);
          var eCoord = new GeoCoordinate(point2.Lat, point2.Long);

          var distance = sCoord.GetDistanceTo(eCoord);

          if(distance > 30)
          {
          var tempList = group.Items.GetRange(rangeStart, i - rangeStart + 1);
          tempLists.Add(tempList);
          rangeStart = i + 1;//Next range will begin on the following item
          }
          }

          if (group.Items.Count - rangeStart > 0)
          {
          //Add all remainging (not added yet) points as the last range.
          var tempList = group.Items.GetRange(rangeStart, group.Items.Count - rangeStart);
          tempLists.Add(tempList);
          }
          }





          share|improve this answer





















          • Thank you, exactly what i was looking for.
            – Tony_89
            Nov 23 at 10:26














          1












          1








          1






          To create a range (using GetRange() method), you need to know where it begins and where it ends. If distance between Item[i] and Item[i+1] is greater then 30, you know the end, because that end is at index i. But you don't know the beginning (of course, you know it for the first range - it's 0), because beginning depends on the end of previous range. So you need to introduce new variable (it's called rangeStart in my example bellow), that will contain such information. It starts with value 0 (that's where first range always begins) and then update it's value whenever you add new range (next range will always start at index i+1).



          After the for loop finishes, some points will remain. So need to add them points as the last range. Whole method can then look like this:



          var unkownSegments = grouped.Where(x => x.ActivityType == null);

          foreach (var group in unkownSegments)
          {
          var tempLists = new List<List<LocationResult>>();

          //This variable keeps track of the beginning of the next range
          var rangeStart = 0;

          for (int i = 0; i < group.Items.Count - 1; i++)
          {
          var point1 = group.Items[i];
          var point2 = group.Items[i + 1];

          var sCoord = new GeoCoordinate(point1.Lat, point1.Long);
          var eCoord = new GeoCoordinate(point2.Lat, point2.Long);

          var distance = sCoord.GetDistanceTo(eCoord);

          if(distance > 30)
          {
          var tempList = group.Items.GetRange(rangeStart, i - rangeStart + 1);
          tempLists.Add(tempList);
          rangeStart = i + 1;//Next range will begin on the following item
          }
          }

          if (group.Items.Count - rangeStart > 0)
          {
          //Add all remainging (not added yet) points as the last range.
          var tempList = group.Items.GetRange(rangeStart, group.Items.Count - rangeStart);
          tempLists.Add(tempList);
          }
          }





          share|improve this answer












          To create a range (using GetRange() method), you need to know where it begins and where it ends. If distance between Item[i] and Item[i+1] is greater then 30, you know the end, because that end is at index i. But you don't know the beginning (of course, you know it for the first range - it's 0), because beginning depends on the end of previous range. So you need to introduce new variable (it's called rangeStart in my example bellow), that will contain such information. It starts with value 0 (that's where first range always begins) and then update it's value whenever you add new range (next range will always start at index i+1).



          After the for loop finishes, some points will remain. So need to add them points as the last range. Whole method can then look like this:



          var unkownSegments = grouped.Where(x => x.ActivityType == null);

          foreach (var group in unkownSegments)
          {
          var tempLists = new List<List<LocationResult>>();

          //This variable keeps track of the beginning of the next range
          var rangeStart = 0;

          for (int i = 0; i < group.Items.Count - 1; i++)
          {
          var point1 = group.Items[i];
          var point2 = group.Items[i + 1];

          var sCoord = new GeoCoordinate(point1.Lat, point1.Long);
          var eCoord = new GeoCoordinate(point2.Lat, point2.Long);

          var distance = sCoord.GetDistanceTo(eCoord);

          if(distance > 30)
          {
          var tempList = group.Items.GetRange(rangeStart, i - rangeStart + 1);
          tempLists.Add(tempList);
          rangeStart = i + 1;//Next range will begin on the following item
          }
          }

          if (group.Items.Count - rangeStart > 0)
          {
          //Add all remainging (not added yet) points as the last range.
          var tempList = group.Items.GetRange(rangeStart, group.Items.Count - rangeStart);
          tempLists.Add(tempList);
          }
          }






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 23 at 0:07









          Ňuf

          4,84921222




          4,84921222












          • Thank you, exactly what i was looking for.
            – Tony_89
            Nov 23 at 10:26


















          • Thank you, exactly what i was looking for.
            – Tony_89
            Nov 23 at 10:26
















          Thank you, exactly what i was looking for.
          – Tony_89
          Nov 23 at 10:26




          Thank you, exactly what i was looking for.
          – Tony_89
          Nov 23 at 10:26


















          draft saved

          draft discarded




















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.





          Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


          Please pay close attention to the following guidance:


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53438583%2fcompare-two-items-in-list-and-then-split-into-smaller-list-at-index%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          A CLEAN and SIMPLE way to add appendices to Table of Contents and bookmarks

          Calculate evaluation metrics using cross_val_predict sklearn

          Insert data from modal to MySQL (multiple modal on website)