Home > Blockchain >  how do I read an array and return an array?
how do I read an array and return an array?

Time:09-21

I'm really new to Python and I'm struggling with what must be a simple problem.

I have an array of numbers between 600 and 900; I want to create a function that returns a text value depending on the value of the numbers.

values = array([702, 664, 817, 893, 768, 789, 637, 642, 619, 724])

def function(x):
      for i in np.nditer(x):
        if 600 <= i <= 699:
          x[i] = ('Low')
        elif 700 <= i <= 799:
          x[i] = ('Med')
        else:
          x[i] = ('High')
        return i 

y = function(value)

CodePudding user response:

Is this what you are looking for?

Code:

values = [702, 664, 817, 893, 768, 789, 637, 642, 619, 724]

def function(x):
    x_out = []
    for i in x:
        if 600 <= i <= 699:
            x_out.append('Low')
        elif 700 <= i <= 799:
            x_out.append('Med')
        else:
            x_out.append('High')
    return x_out

y = function(values)
print(values)
print(y)

Output:

[702, 664, 817, 893, 768, 789, 637, 642, 619, 724]
['Med', 'Low', 'High', 'High', 'Med', 'Med', 'Low', 'Low', 'Low', 'Med']

CodePudding user response:

I think i understand what you want, for each value in the initial array, print out its corresponding text ("low","mid" or "high"). Just modify your code a little.

def function(array):
      output = []
      for value in array:
        if 600 <= value <= 699:
          output.append('Low')
        elif 700 <= value <= 799:
          output.append('Med')
        else:
          output.append('High')
      output

Despite you are new with Python, the code above is simple to read/understand, hope so. You can play with the function.

values = array([702, 664, 817, 893, 768, 789, 637, 642, 619, 724])
output = function(value)
print(output)
  • Related