Write Python code equivalent to the following:
even_odd = np.where(h % 2 == 0, 'even', 'odd')
You should use nested for-loop and if/else statement.
You can start your code as the below:
even_odd = np.empty_like(h, dtype="<U4")
for i in range(h.shape[0]): # i is row index
for j in range(h.shape[1]): # j is column index
You should get the results below:
array([['even', 'odd', 'even'],
['odd', 'even', 'odd'],
['even', 'odd', 'even'],
['odd', 'even', 'odd'],
['even', 'odd', 'even']], dtype='<U4')
My Code Input:
even_odd = np.empty_like(h, dtype="<U4")
for i in range(h.shape[0]):
for j in range(h.shape[1]):
if (h[i, j] % 2 == 0):
h[i, j]
else:
h[i, j]
Output:
even_odd
array([['', '', ''],
['', '', ''],
['', '', ''],
['', '', ''],
['', '', '']], dtype='<U4')
I am trying to breakdown the code but cannot seem to find the solution. I am confused on how to get "even" or "odd" to show up on my array.
Under my if/else statement, I tried print("even") and print("odd") but I get the list above the array as my output:
even
odd
even
odd
even
odd
even
odd
even
odd
even
odd
even
odd
even
array([['', '', ''],
['', '', ''],
['', '', ''],
['', '', ''],
['', '', '']], dtype='<U4')
I feel like I am close as I simply need to find a way to get the list content in the array. I researched similar questions on here but all of them seem to be from going from for-loop and if/else statement to np.where, I am trying to do the opposite.
Any insight/help would be greatly appreciated, trying to master the basics!
You forgot to assign values to your matrix!
even_odd = np.empty_like(h, dtype="<U4")
for i in range(h.shape[0]):
for j in range(h.shape[1]):
if (h[i, j] % 2 == 0):
even_odd[i, j]='even'
else:
even_odd[i, j]='odd'
print(even_odd)
Firebase Cloud Functions: PubSub, "res.on is not a function"
TypeError: Cannot read properties of undefined (reading 'createMessageComponentCollector')
I know there's a way to merge lists into a dict by zip function, but once the 'key-list' contains duplicated factors it doesn't works well since many values will be cutI wonder if there is a good solution to merge lists with the same keys?
I face an issue which is that my first button is using the second button's commandI have faced this logic error multiple times when trying to create buttons programmatically with different functions, is there a way to resolve this or is this a limitation...
I'm trying to merge and then update a graph using the py2neo libraryMy code looks roughly like
In order to make data anonymous, I need to replace original IDs with a different set of new IDs, but still have identical original IDs match across all fields after being replacedThe challenge is doing so elegantly across 4 different presentations of IDs in this Pandas...