Breaking down np.where with nested for-loop and if/else statement

119
April 13, 2022, at 11:20 PM

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!

Answer 1

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)
Rent Charter Buses Company
READ ALSO
how to merge two lists with duplacated factors into a dict in python? [duplicate]

how to merge two lists with duplacated factors into a dict in python? [duplicate]

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?

87
Tkinter Button Command switches when new Button is used [duplicate]

Tkinter Button Command switches when new Button is used [duplicate]

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...

142
Updating a node with merge using Py2Neo

Updating a node with merge using Py2Neo

I'm trying to merge and then update a graph using the py2neo libraryMy code looks roughly like

95
Replacing numerical IDs contained as lists of strings and lists of ints in a Pandas DataFrame

Replacing numerical IDs contained as lists of strings and lists of ints in a Pandas DataFrame

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...

131