Find nearest value in numpy array

import numpy as np def find_nearest(array, value): array = np.asarray(array) idx = (np.abs(array – value)).argmin() return array[idx] array = np.random.random(10) print(array) # [ 0.21069679 0.61290182 0.63425412 0.84635244 0.91599191 0.00213826 # 0.17104965 0.56874386 0.57319379 0.28719469] value = 0.5 print(find_nearest(array, value)) # 0.568743859261

Python list of dictionaries search

You can use a generator expression: >>> dicts = [ … { “name”: “Tom”, “age”: 10 }, … { “name”: “Mark”, “age”: 5 }, … { “name”: “Pam”, “age”: 7 }, … { “name”: “Dick”, “age”: 12 } … ] >>> next(item for item in dicts if item[“name”] == “Pam”) {‘age’: 7, ‘name’: ‘Pam’} If … Read more

How to search by key=>value in a multidimensional array in PHP

Code: function search($array, $key, $value) { $results = array(); if (is_array($array)) { if (isset($array[$key]) && $array[$key] == $value) { $results[] = $array; } foreach ($array as $subarray) { $results = array_merge($results, search($subarray, $key, $value)); } } return $results; } $arr = array(0 => array(id=>1,name=>”cat 1″), 1 => array(id=>2,name=>”cat 2″), 2 => array(id=>3,name=>”cat 1″)); print_r(search($arr, ‘name’, … Read more

How can I combine two or more querysets in a Django view?

Concatenating the querysets into a list is the simplest approach. If the database will be hit for all querysets anyway (e.g. because the result needs to be sorted), this won’t add further cost. from itertools import chain result_list = list(chain(page_list, article_list, post_list)) Using itertools.chain is faster than looping each list and appending elements one by … Read more

ASP.NET MVC 2.0 Implementation of searching in jqgrid

Probably you have problem on the server side. Could you append your question with the code of DynamicGridData action which you currently use. The action should have filters as the parameter. Some parts of your current code are definitively wrong. For example jqGrid is the jQuery plugin. So the methods of jQuery will be extended … Read more