Get n (part of) objects from a list of objects, starting from n index

Related searches

I have a list of n objects. I want to grab a maximum of n items (-n previous items and n + next items), starting from the selected index. I want a method for this, in which I provide the list with certain parameters.

How can I achieve this in C#?

First example: int selectedIndex = 5 int itemsToTake = 4 (2 previous and 2 next) Returned list = 3-4-5-6-7 Second example: int selectedIndex = 1 int itemsToTake = 4 (take 1 previous and 3 next, because list starts with 0) Returned list = 0-1-2-3-4

I have already tried a combination of list.skip().take(), only that didn't go so well.

Example:

nextIndex = nextIndex - prevIndex + 1;
return List.Skip (prevIndex) .Take (nextIndex) .ToList ();

An inefficient but visually aesthetic way of doing it:

public static IEnumerable<T> Nearby<T>(this IEnumerable<T> source,
    int selectedIndex, int itemsToTake)
{
    var left = source.Take(selectedIndex).Reverse().Take(itemsToTake / 2).Reverse();
    var middle = source.ElementAt(selectedIndex);
    var right = source.Skip(selectedIndex).Skip(1).Take(itemsToTake / 2);
    return left.Append(middle).Concat(right);
}

Usage example:

var source = Enumerable.Range(0, 10);
Console.WriteLine($"Result: {String.Join(", ", source.Nearby(5, 5))}");

Output:

Result: 3, 4, 5, 6, 7

11. Lists — How to Think Like a Computer Scientist: Learning with , We will use the term element or item to mean the same thing. Remember that the indices start at 0: need or use the index i for anything besides getting the items from the list, Since variables refer to objects, if we assign one variable to another, both Write a function to return a list of all prime numbers less than n:� The List<T> is searched forward starting at index and ending at index plus count minus 1, if count is greater than 0. This method determines equality using the default equality comparer EqualityComparer<T>.Default for T, the type of values in the list. This method performs a linear search; therefore, this method is an O(n) operation, where n is

Skip + Take should work fine, try this:

int firstIndex = selectedIndex - itemsToTake / 2;
firstIndex = firstIndex < 0 ? 0 : firstIndex;
return list.Skip(firstIndex).Take(itemsToTake);

ArrayList (Java Platform SE 7 ), The size, isEmpty, get, set, iterator, and listIterator operations run in constant time. The add operation runs in amortized constant time, that is, adding n elements If no such object exists, the list should be "wrapped" using the Collections. Returns the index of the first occurrence of the specified element in this list, or -1 if � When using Rmpi to send processes to many nodes, it is convenient to create a list of tasks that are assigned to nodes as they become available. In my case, I was working through a large factorial set of simulations and needed to use a unique set of va

First, make sure that the list contains enough elements:

if(itemsToTake  + 1 > List.Count)
    return List.ToList(); //copy the list

The first index you want to take is (not considering sequence borders):

var firstIndex = selectedIndex - itemsToTake  / 2;

The corresponding last index would be firstIndex + n.

Then, make sure that the range is valid

if(firstIndex < 0) 
    firstIndex = 0;
if(firstIndex + nitemsToTake >= List.Count)
    firstIndex = List.Count - 1 - itemsToTake ;

And finally do as you tried:

return List.Skip(firstIndex).Take(itemsToTake  + 1).ToList();

List Rendering — Vue.js, <ul id="example-2"> <li v-for="(item, index) in items"> {{ parentMessage }} - {{ index } new Vue({ el: '#v-for-object', data: { object: { title: 'How to do lists in Vue', author: 'Jane <ul v-for="set in sets"> <li v-for="n in even(set)">{{ n }}</li> </ul> your_list = [1, 2, 3] print("Hello number, " + your_list[0]) » Hello number, 1. Formula: item number on list -1 So, item number 2 on your_list is in position 1.

You need to handle the special case of when selectedIndex - itemsToTake / 2 is less than 0:

public static List<T> Take<T>(this List<T> list, int selectedIndex, int itemsToTake) {
    if (selectedIndex - n / 2  < 0) {
        return list.Take(itemsToTake + 1).ToList();
    }
    return list.Skip(selectedIndex - itemsToTake / 2).Take(itemsToTake +1).ToList();
}

Indexing — NumPy v1.19 Manual, The basic slice syntax is i:j:k where i is the starting index, j is the stopping If the number of objects in the selection tuple is less than N, then : is You may use slicing to set values in the array, but (unlike lists) you can never grow the array. integers) and the subspace from the advanced indexing part. Dictionary object number of the segment that contains the object Note: OBJECT_ID and DATA_OBJECT_ID display data dictionary metadata. Do not confuse these numbers with the unique 16-byte object identifier ( object ID ) that the Oracle Database assigns to row objects in object tables in the system.

public static IEnumerable<T> Nearby<T>(IEnumerable<T> source, int selectedIndex, int itemsToTake)
{
  itemsToTake = ((itemsToTake/2)*2)+1;
  Queue<T> queue = new Queue<T>();
  bool foundItem = false;
  int afterItemCount = 0;
  int recommendedAfterItemCount = itemsToTake/2;
  foreach(var pair in source.Select((t, i) => new {t, i}))
  {
    T t = pair.t;
    int i = pair.i;

    queue.Enqueue(t);
    if (itemsToTake < queue.Count) queue.Dequeue();

    if (i == selectedIndex) foundItem = true;
    else if (foundItem) afterItemCount += 1;

    bool enoughItems = queue.Count == itemsToTake;
    bool enoughAfterItems = recommendedAfterItemCount <= afterItemCount;
    if (enoughItems && enoughAfterItems) break;

  }
  foreach(T t in queue)
  {
    yield return t;
  }
}

Documentation, Creates a slice of array with n elements dropped from the beginning. objects for ['barney', 'fred', 'pebbles'] Gets the index at which the first occurrence of value is found in array using var say = _.rest(function(what, names) { in HTML and have no special meaning unless they're part of a tag or unquoted attribute value. The reason why we use zero here is because the first item is at the beginning of the list so we use an offset of 0 items to get to it. To get to the second item, we would need to use an offset of 1 to skip the first item. PS> $data One This would mean that the last item is at offset 3.

The following example demonstrates the GetRange method and other methods of the List<T> class that act on ranges. At the end of the example, the GetRange method is used to get three items from the list, beginning with index location 2. The ToArray method is called on the resulting List<T>, creating an array of three elements. The elements of

C# List class represents a collection of strongly typed objects that can be accessed by index. In this tutorial, we learn how to work with lists in C# using C# List class to add, find, sort, reverse, and search items in a collection of objects using List class methods and properties.

get(int index) Returns the object at the specified position in the list. indexOf(Object elem) Returns the index position of the first occurrence of the specified object in the array list. If the object isn’t in the list, it returns -1. isEmpty() Returns a Boolean value that indicates whether the array list is empty. iterator()