Filling a list with values in a range

python generate list of numbers with step size
python initialize list with range
generate list of numbers python
python range
create a list of 100000 integers with values between 1 and 1000 in python
how to check if a range of values is in a list python
how to add range to a list python
python list interval

I have a list of dates like below :

2017-02-08 00:00:00.000
2017-02-08 00:00:00.000
NULL
2017-03-20 00:00:00.000
NULL
2017-03-20 00:00:00.000
NULL
NULL
2017-03-20 00:00:00.000
2017-02-08 00:00:00.000
NULL
NULL
NULL
2017-02-08 00:00:00.000

This should be filled like this. that is to fill all dates that has similar neighbors up to 2 NULL values.

2017-02-08 00:00:00.000
2017-02-08 00:00:00.000
NULL
2017-03-20 00:00:00.000
2017-03-20 00:00:00.000
2017-03-20 00:00:00.000
2017-03-20 00:00:00.000
2017-03-20 00:00:00.000
2017-03-20 00:00:00.000
2017-02-08 00:00:00.000
NULL
NULL
NULL
2017-02-08 00:00:00.000

Here is what I build so far. Any idea is appreciated

DateTime? latestDate = null;

foreach (var date in dates)
{
    if (date == null)
    {
        latestDate = date;
    }
    else
    {
        date = latestDate;
    }
}

For simplification and to not deal with string parsing and date comparaison. In order to have a clear mre, DateTime are now int. Live demo

Test case.

Base on the question I have the following test case:

//Input                           Expected Output
{ null, 1 },                    { null, 1 } 
{ null, null, 1 },              { null, null, 1 } 
{ null, null , null, 1 },       { null, null , null, 1 } 

{ 1, null, 1 },                 { 1, 1, 1 } 
{ 1, null, null, 1 },           { 1, 1, 1, 1 } 
{ 1, null, null, null, 1 },     { 1, null, null, null, 1 } 

{ 1, null, 2 },                 { 1, null, 2 } 
{ 1, null, null, 2 },           { 1, null, null, 2 } 
{ 1, null, null, null, 2 },     { 1, null, null, null, 2 } 
The code.

Here we won't look for code optimisation, minimum variable assignation or keeping the last value we just read. We stay close to the test case.

for (int i = 0; i < input.Length - 1; i++)
{
    if (i == 0) { continue; } //First elment? Skip.
    if (input[i] != null) { continue; }// Already have a value? Skip.

    // First and last elment are safe.
    int? closestValueLeft = input[i - 1];
    int? closestValueRight = input[i + 1];

    // ####  CASE 1 #### : Left and right have value. 
    if (closestValueLeft != null && closestValueRight != null)
    {
        if (closestValueLeft == closestValueRight)
        {// both are the same. 
            input[i] = closestValueLeft;
        }
    }

    // ####  CASE 2 #### : Left and right don't have value. 
    else if (closestValueLeft == null && closestValueRight == null)
    {// Left and right have no value, Skip.
        continue;
    }

    // ####  CASE 3 #### : We have to move a bit to find 
    else
    {// Either left or right have a value but not both. We are gona move one step after the null. 
        if (closestValueLeft != null)
        { // Left is not null move right. 
            if (i + 2 < input.Length)// Array out of bound protection
            {
                closestValueRight = input[i + 2];
            }
        }
        else
        {// Right is not null, move left. 
            if (i - 2 > 0)// Array out of bound protection
            {
                closestValueLeft = input[i - 2];
            }
        }

        if (closestValueLeft == closestValueRight)
        {
            input[i] = closestValueLeft;
        }
    }
}

Python, Python comes with a direct function range() which creates a sequence of numbers from start to stop values and print each item in the sequence. We use range()  Click the cell range button again to return to the full dialog box. Now, click “Import”. The custom list is added to the Custom lists box and you can select it and edit the list in the List entries box, if you want. Click “OK”. You can fill cells with your custom list using the fill handle, just like you did with the first method above.

I haven't tested this as not recreated the List of dates, but this (or something pretty close) should work

if (dates.Count > 2)
{
      int i = 0;
      do
      {
          if (dates[i] != null && dates[i + 1] == null && dates[i + 2] == null && (i + 3 >= dates.Count || dates[i + 3] != null))
          {
              dates[i + 1] = dates[i];
              dates[i + 2] = dates[i];
              i += 3;
          }
          else i++;
      } while (i < dates.Count - 3);
  }

EDIT: I may have misunderstood the original requirement (see comment). If so, this may be more suitable

        if (dates.Count > 2)
        {
            int i = 0;
            do
            {
                if (dates[i] != null && dates[i + 1] == null && dates[i + 2] == dates[i])
                {
                    dates[i + 1] = dates[i];
                    i += 2;
                }
                else if (dates[i] != null && dates[i + 1] == null && dates[i + 2] == null && (i+3<dates.Count && dates[i + 3] == dates[i]))
                {
                    dates[i + 1] = dates[i];
                    dates[i + 2] = dates[i];
                    i += 3;
                }
                else i++;
            } while (i < dates.Count - 3);
        }

Scala List class examples: range, fill, tabulate, appending, foreach , The range function can also take a third argument which serves as a "step" value when creating the List : scala> val x = List.range(0,10,2) x:  define the range; assign the range to the list box’s list property; and that is it 🙂 . Before jumping right to it, it is worth to consider the following: whether the range is a one column or multi column range; are you dealing with a fixed or changing range (if it is a changing range I suggest to use tables or dynamic names or an array)

If I understand the question, this is the solution:

foreach (var date in dates.Select((date, index) => new { date, index }))
{
    if (date.index - 2 < 0) continue;

    if (dates[date.index - 2].HasValue && 
        dates[date.index - 2].Value == date.date && 
       !dates[date.index - 1].HasValue)
    {
        dates[date.index - 1] = date.date;
    }
    else if (dates[date.index - 3].HasValue &&
             dates[date.index - 3].Value == date.date &&
            !dates[date.index - 1].HasValue &&
            !dates[date.index - 2].HasValue)
    {
        dates[date.index - 1] = date.date;
        dates[date.index - 2] = date.date;
    }
}

@Drag and Drop thx good idea

for (int index = 0; index < dates.Count(); index++)
{
    if (index - 2 < 0) continue;

    if (dates[index - 2].HasValue &&
        dates[index - 2].Value == dates[index] &&
       !dates[index - 1].HasValue)
    {
        dates[index - 1] = dates[index];
    }
    else if (index - 3 >= 0 &&
             dates[index - 3].HasValue &&
             dates[index - 3].Value == dates[index] &&
            !dates[index - 1].HasValue &&
            !dates[index - 2].HasValue)
    {
    dates[index - 1] = dates[index];
    dates[index - 2] = dates[index];
}

How to populate a Java int array with a range of values , Here's an easy way to populate/initialize a Java int array with data, such as a range of numbers. The key is to use the rangeClosed method on  > Fill a List Box with unique values from a worksheet using VBA in Microsoft Excel In this article, we will create a List Box in userform and load it with values after removing duplicate values. Raw data which we will insert in the List Box, consists of names.

Python : How to create a list and initialize with same values , Creating a list of same values by List Comprehension with range(). This is an another way to create a list of same value using range() i.e.. This post also covers a method to remove duplicates from a range. It's one of the most common data crunching task in Excel. Scenario Suppose you have a list of customer names. The list has some duplicate values. You wish to extract unique values from it. Unique values would be a distinct list.

How to Show a List of Values in a Cell in Excel, Select the range of cells containing the values. As you select the cells, the cell addresses automatically populate the "Source" field in the dialog box. 2. Keep selecting cell D2, then drag the Fill Handle down to get all unique values from the specified range. Now all unique values in column range B2:B9 are extracted. When values in this range changed, the unique value list will be dynamically changed immediately.

Constructing Lists—Wolfram Language Documentation, Range — form a list from a range of numbers or other objects {1,2,3,…} Table — make a table of any dimension of values of an expression. Array — make an  1. Select the range you want to list all numbers between given numbers, and then click Kutools > Insert > Fill Blank Cells. See screenshot: 2. In the Fill Blank Cells dialog box, select the Linear values option in the Fill with section, and choose From top to bottom option in the Options section, and finally click the OK button.

Comments
  • Could you clarify similar neighbors up to 2 NULL values?
  • For the following array { null, null, 1 } will the result be { 1, 1, 1 } or { null, null, 1 } ?
  • @DragandDrop it should be { null, null, 1 } since there is no left neighbor.
  • @RoadRunner basically if there is any null between 2 similar dates, it will be filled with that date. and the number of in between nulls are restricted to 2
  • May you rules this case for me will { 1, null, null, 2 } give { 1, null, null, 2 } or { 1, 1, 1, 2 }? And if you find the time tellm me if the case in the first paragraphe of my answer are correct.
  • Any thoughts on how to make this neighbors up to 2 number configurable
  • Configurable? Like 1 or 2 or 4-99
  • In case 3 after the Letfnotnull and right not null. You can add a for loop looping from 2 to infinity. You keep the out of bound You just change 2 by new counter
  • Well {1, null, null, 2} will return {1, 1, 1, 2}. In Op exemple lines 3 , we have {1, null, 2} that stay that way. While this exemple work in your case, and as {1, null, null, 2} is not in the give exemple we cannot say at 100% if it work or not. Gentleman way to say; in a case not documented I think it does not.
  • @DragandDrop true, but that's how I understood the requirement from the OP, i.e. to replace two null values with previous date. I this the requirement could be up to two null values between matching dates. I've amended answer to include this.
  • if (date.index - 3 < 0) Is not really intuitive too. And array with {1,null,1}. Will be ignored.
  • I edited your answers as (date.index - 3 < 0) will simply return a wrong result. And you corrected that in the second part. But a quick question: Your answer is right to the point and the algo is correct. But, Do you really find ( XYZ - 2 < 0) more readable than ( XYZ < 2 ). It's not a question about style, I just haven't found anyone that wrote it like that before.