searching a value in a ndarray and then printing the rest of the rest of the array - numpy-ndarray

If an ndarray is data=np.array([[50,49,48,47,46],[25,5,3,6,5]]...)
In the same way as I may I could index data[0,1]
how can I print or manipulate the array when the value is specific, such as 48 in the example above to print [48, 3]
Have tried np.where and np.take
print((np.where(48),data[0,:]))
a = np.where(48(data))
print(a)

Related

Ruby: deleting object while looping over list with that object

So I have multiple lists to keep track of objects in a 2D game, but if these objects go off screen I want to remove these objects so they are no longer updated. What I have below works for me, but this doesn't work in other languages. Usually I have to make another "destroy list" that saves the objects I want to destroy and then loop again to remove them, because you can't remove an object from the list while iterating without some visible glitch.
Is Ruby just not showing any visible glitch while doing this or does Ruby's array work differently when removing multiple possible objects from a list while it's still iterating?
objects = []
objects.each{|o| o.withinBounds ? o.update : objects.delete(o)}
In Ruby you will actually find a glitch if you do what you are saying.
Try this:
objects = [1,2,3,4]
objects.each { |el| objects.delete(el) }
=> [2, 4]
You would expect the result to be an empty array, but is not. We are messing up with the elements of the arr and each gets confused, because the length of the array has changed. The each iterator looks something like this in pseudocode:
count = 0
while length(objects) > count
yield objects[count]
count + 1
end
So, in the example I shown above, the reason why we get [2, 4] can be explained on a step by step analysis on what objects.each { |el| objects.delete(el) } is doing:
We start with 4 (length of objects) > 0.
Number 1 is yielded, and deleted.
count = 1
3 (length of objects) > 1
Number 3 is yielded and deleted.
count = 2
2 (length of objects) is not bigger than count
We are done, so we have [2, 4]
There is a better way to do what you are trying, by using delete_if:
new_objects = objects.delete_if {|o| o.withinBounds }

Get item from array based on hashed string in Ruby

I would like to extract an element from an array, predictably based on a string input.
My use case: Generate a color for a user, based on name.
One (faulty) implementation would be to generate a hash from the user name, and reduce it to a value between 0-25 like so:
def color
colors = array_of_25_colors
colors[Digest::MD5.hexdigest(name).to_i(16) & 25]
end
This implementation only returns 8 possible colors, so no good.
EDIT
Another implementation I've tried is to use the byte code from the first character in the name, but since I allow unicode in names this is not practical:
"aaron".first.bytes.first - 97
=> 0 #Okay!
"zac".first.bytes.first - 97
=> 25 #Nice!
"örjan".first.bytes.first - 97
=> 98 #Doh!
What could a working implementation look like?
You can add up the ordinal values of each character in the string then divide mod 25 like this:
colors = array_of_25_colors
colors[name.bytes.reduce(:+) % 25]
Building on your use of #bytes, you could also go with something like:
colors[name.bytes.inject(:+) % 25]

Parse.com query objects where the key's array value contains any of the elements

on https://parse.com/docs/js_guide#queries-arrays there is an example how to find objects where the key's array value contains each of the elements 2, 3, and 4 with the following:
// Find objects where the array in arrayKey contains all of the elements 2, 3, and 4.
query.containsAll("arrayKey", [2, 3, 4]);
However, I would like to find objects where the key's array value contains at least one (not necessarily all) of the elements 2,3, and 4.
Is that possible?
I'm not positive, but what happens if you try containedIn?
I think if you pass an array, it checks to see if any are contained.
query.containedIn("arrayKey", [2,3,4]);
I know that if you use equalTo with an array key and a singular value, it checks if the value is in the array and returns TRUE. I think this will do something similar and should work. I think it will check if any value in "arrayKey" is in the passed array. If any key object does, it will return the object.
swift 3.0
let Query:PFQuery = PFQuery(className: “className”)
Query.whereKey(“Field Name”, containedIn: array)// [“1”,”2”,”3”];

How to implement slice_after (or group certain elements with certain subsequent ones) in Ruby?

The Enumerable#slice_before method is quite useful, and it does exactly what it says on the tin - slice an array before an element if a certain condition on the element is met. For example, I am using it to group certain numbers to the following ones.
In my case, the IDs 0xF0 to 0xFB should be grouped with the IDs that come after them, including multiple of these IDs in a row (they are "modifier" flags in the thing I'm making). I'm doing it like this:
# example code (for SSCCE)
code = '00FF1234F0AAF0BBF0CCCCF3F4F5AAAAAA'.split('').each_slice(2).map{|n| n.join.to_i 16 }
# grouping the modifiers (more may be added later, so array is used)
code = code.slice_before {|tkn| ![*0xF0..0xFB].include? tkn }.to_a
The result of code after this is
[[0], [255], [18], [52, 240], [170, 240], [187, 240], [204], [204, 243, 244, 245], [170], [170], [170]]
However, the desired result is
[[0], [255], [18], [52], [240, 170], [240, 187], [240, 204], [204], [243, 244, 245, 170], [170], [170]]
I found this entry on bugs.ruby-lang.org, and the response was
The main reason [that this is not implemented] is no one requested.
I have not enough time to implement it now.
Therefore, how can I implement it myself?
Enumerable#slice_after is available if you are using Ruby 2.2.0 or later, so you can just use it:
modifiers = 0xF0..0xFB
hex_code = '00FF1234F0AAF0BBF0CCCCF3F4F5AAAAAA'
bytes = [hex_code].pack('H*').bytes
code = bytes.slice_after { |b| !modifiers.include? b }.to_a
p code # => [[0], [255], [18], [52], [240, 170], ...
It's not the elegant one-liner I'd like to have, but this gets the job done :)
target = []
code.each do |i|
# if there is a previous element and it was one of the "modifiers"
if target.last && [*0xF0..0xFB].include?(target.last.last)
# append it to the current subarray
target.last << i
else
# otherwise, append a new subarray
target << [i]
end
end
You'll find the desired array in target with code being unchanged.

Apply function to each element in array and store result in an array

I have a function toWords which converts a integer into a word
e.g. toWords(500, tableWords) gives fivehundred
I have an array of numbers h = (1..999).to_a, and I want to go through this array and convert each number into a word and store it in a new array. My current attempt to do this is:
h = (1..999).to_a
Lh = h.each do |i| toWords(i, tableWords) end
However, the contents of Lh is simply the integers from 1 to 999 and not the output of my toWords function. How do I do this? I'm thinking of something along the lines of sapply in R.
Even better is if my new array Lh can have two columns, the first column containing the integers in number format, and the second column would be the corresponding number in words.
Thank you!
To get your two columns, you can do the following
(1..999).map {|x| [x, toWords(x, tableWords)]}
As per Cicada's comment, the answer is:
Lh = h.map{|x| toWords(x, tableWords)}

Resources