简体   繁体   中英

How to find a 'str' in a 2-D array and return element in next column?

I've successfully combined two lists (A, B) using numpy.array and assigned it to a new variable. The first list contained strings (A) and the second contained integers (B). When I print the new variable (C) I get:

    [[Str, Str]
     [Int, Int]]

I'm now trying to find the corresponding integer if the str == 'XYZ'. How do I use the index function to find the int in the row next to 'XYZ'?

My initial thought was to assign the new list to a variabe ie D:

  D = C[A == 'XYZ',]

However, I only get True, False, etc.

Sorry, I know this is quite basic.

这是您要完成的工作吗?

import numpy as np my_matrix = np.array([['ABC','XYZ'],[1,99]]) print(my_matrix) my_index = my_matrix[0]=='XYZ' print(my_matrix[1][my_index])

Here's what I came up with:

  import numpy as np 
  A = ['ABC', 'XYZ']
  B = [1, 99]
  C = B[A == 'ABC']
  D = B[A != 'ABC']
  print(C)
  print(B)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM