Algorithm to determine indices i..j of array A containing all the elements of another array B
Date : March 29 2020, 07:55 AM
may help you . Complexity Time: O((m+n)log m) Space: O(m) The following is provably optimal up to a logarithmic factor. (I believe the log factor cannot be got rid of, and so it's optimal.)
|
Convert an array to hash, where keys are the indices
Tag : ruby , By : itsmegb
Date : March 29 2020, 07:55 AM
this will help I am transforming an array into a hash, where the keys are the indices and values are the elements at that index. arr = ["one", "two", "three", "four", "five"]
x = Hash[(0...arr.size).zip arr]
# => {0=>"one", 1=>"two", 2=>"three", 3=>"four", 4=>"five"}
|
Access deep array value in php using an array of keys/indices in php?
Tag : php , By : FuzzyHornet
Date : March 29 2020, 07:55 AM
I hope this helps you . First of all (you have typo array('c' = 'value'), should be array('c' => 'value'),) I tried to use your code: $object = array ('a' => array ( 'b' => array('c' => 'value'), 'd' => 3));
$indices = array ('a', 'b', 'c');
echo accessObjectKey($object,$indices);
function getByPath($arr,$path) {
if (!isset($arr[$path[0]])) {
return 'There is no '.$path[0].' element!';
} elseif(count($path)==1) {
return $arr[$path[0]];
} elseif(!is_array($arr[$path[0]])) {
return 'Element '.$path[0].' is not array! ';
} else {
$key = array_shift($path);
return getByPath($arr[$key],$path);
}
}
$object = array ('a' => array ( 'b' => array('c' => 'value'), 'd' => 3));
$indices = array ('a', 'b', 'c');
echo getByPath($object,$indices);
|
How do I use an array of named indices (keys) to set a value in a nested hash?
Tag : ruby , By : yew tree
Date : March 29 2020, 07:55 AM
I think the issue was by ths following , Much simpler approach(in my opinion) is to access elements successively with :[]: keys = [:canada, :ontario, :ottawa]
hash = { canada: { ontario: { ottawa: :me}, manitoba: { winnipeg: nil} }, united_states: { district_of_coloumbia: { washington: nil } } }
# get
p keys.inject(hash) { |h, k| h.public_send(:[], k) }
#=> :me
# set
last = keys[0..-2].inject(hash) { |h, k| h.public_send(:[], k) }
last.public_send(:[]=, keys[-1], 'other')
p hash #=> {:canada=>{:ontario=>{:ottawa=>"other"}, :manitoba=>{:winnipeg=>nil}}, :united_states=>{:district_of_coloumbia=>{:washington=>nil}}}
def get_by_keys(hash, keys)
keys.inject(hash) { |h, k| h.public_send(:[], k) }
end
def set_by_keys(hash, keys, v)
last = keys[0..-2].inject(hash) { |h, k| h.public_send(:[], k) }
last.public_send(:[]=, keys[-1], v)
hash
end
keys = [:canada, :ontario, :ottawa]
hash = { canada: { ontario: { ottawa: :me}, manitoba: { winnipeg: nil} }, united_states: { district_of_coloumbia: { washington: nil } } }
p get_by_keys(hash, keys) #=> :me
p set_by_keys(hash, keys, 'other') #=> {:canada=>{:ontario=>{:ottawa=>"other"}, :manitoba=>{:winnipeg=>nil}}, :united_states=>{:district_of_coloumbia=>{:washington=>nil}}}
|
Determine indices of entries in an array that start with a certain string
Tag : python , By : negonicrac
Date : March 29 2020, 07:55 AM
hop of those help? @Divakar's answer is the way to go, but just as an alternative, you can also use a list comprehension: a = np.array(['test1234', 'testworld', 'hello', 'mynewcar', 'test5678'])
[i for i, si in enumerate(a) if si.startswith('test')]
[0, 1, 4]
np.array([i for i, si in enumerate(a) if si.startswith('test')])
|