Find the position of a List Item in a List based upon specifice criteria using LINQ
Tag : chash , By : chawei
Date : March 29 2020, 07:55 AM
this one helps. Something similar to linq find in which position is my object in List, except his accepted answer is evaluating at an object level. , You could use something like: public static IEnumerable<int> GetIndices<T>(this IEnumerable<T> items, Func<T, bool> predicate)
{
return items.Select( (item, index) => new { Item = item, Index = index })
.Where(p => predicate(p.Item))
.Select(p => p.Index);
}
|
Find all the indexes of an item within a list using stream API
Tag : java , By : Alex Sadzawka
Date : March 29 2020, 07:55 AM
hop of those help? For a start, using Lambdas is not the solution to all problems... but, even then, as a for loop, you would write it: List<Integer> results = new ArrayList<>();
for (int i = 0; i < list.size(); i++) {
if (search == list.get(i).intValue()) {
// found value at index i
results.add(i);
}
}
List<Integer> list = Arrays.asList(10, 6, 16, 46, 5, 16, 7);
int search = 16;
int[] indices = IntStream.range(0, list.size())
.filter(i -> list.get(i) == search)
.toArray();
System.out.printf("Found %d at indices %s%n", search, Arrays.toString(indices));
Found 16 at indices [2, 5]
|
Python Fastest way to find Indexes of item in list
Date : March 29 2020, 07:55 AM
it fixes the issue If one was to attempt to find the indexes of an item in a list you could do it a couple different ways here is what I know to be the fastest def find(target, myList):
for i in range(len(myList)):
if myList[i] == target:
yield i
def find_with_list(myList, target):
inds = []
for i in range(len(myList)):
if myList[i] == target:
inds += i,
return inds
In [8]: x = range(50)*200
In [9]: %timeit [i for i,j in enumerate(x) if j == 3]
1000 loops, best of 3: 598 us per loop
In [10]: %timeit list(find(3,x))
1000 loops, best of 3: 607 us per loop
In [11]: %timeit find(3,x)
1000000 loops, best of 3: 375 ns per loop
In [55]: %timeit find_with_list(x,3)
1000 loops, best of 3: 618 us per loop
|
find item in list , and get other item from other list at same location using linq
Date : March 29 2020, 07:55 AM
it fixes the issue correct me if i am wrong are you looking for the index of the item you searched in first list and then use the same index to retrieve from other list If yes Try this var testFirstList = myListCollectionObj.firstList.Where(x => x == 3).FirstOrDefault(); //then i want to get "33", and 333 from secondList and thirdList respectively
var index = myListCollectionObj.firstList.IndexOf(testFirstList);
|
How to find all the indexes of a recurring item in a list? (Python)
Date : March 29 2020, 07:55 AM
|