0

I'm a Python beginner who is starting to get familiar with the pymc library. In my program I'm generating random numbers between 1 and 100. When I'm generating random variables, it obviously returns an array with integers (in this case a 10 int array):

customer_names = customer_names.random(size=10)

The history of this post is that I want to relate each int variable to a specific name through a table, as if each name had an identifier, but I don't know how to implement it in the code.

What I want is that when I print(customer_names) instead of getting an array [ 54 2 45 75 22 19 16 34 67 88] I want to get [ Jose Maria Carmen Alejandro Davinia Eduardo Carlos Fátima Marc Mireia].

I have tried the following:

def identificar_nombres(nc = customer_names):
    for i in range (0, 9): #in this case 9 because I'm generating 10 random numbers
        if nc[i] == '1':
            return 'ANTONIO' 
        elif nc[i] == '2':
            return 'MARIA CARMEN'
        else: return nc[i] # process repeatedly with all names in the name dataset

nombres = identificar_nombres(customer_names) 

but without result.

Thanks!

3
  • 1
    What you can do create a dictionary of names like this dictionary = {"1": 'ANTONIO', "2": "MARIA CARMEN"} etc. and then access the names from the dictionary using dictionary[str(nc[i)] Commented Feb 25, 2020 at 15:05
  • @RishabhBatra Thankyou so much, another stupid thing, how can transform an array of random numbers, to an array transformed by the dictionary with the names ? Commented Feb 25, 2020 at 16:34
  • I found it, problem fixed ;) @RishabhBatra Commented Feb 25, 2020 at 17:19

3 Answers 3

0

Use a dictionary(key-value pair) to store the variables like this:

variables = {1: 'ANTONIO', 2: 'MARIA CARMEN'}

And access it like this:

customer_names = list(variables.values()))
print(customer_names)

Output:

['ANTONIO', 'MARIA CARMEN']
Sign up to request clarification or add additional context in comments.

2 Comments

Thankyou so much Suman, only another stupid doubt, but if I have an array of integers with 10 random numbers, we can call it 'array_indicators'. How can I transform, using that dictionary, that array to another called 'names array' with the values of the dictionary (in this case, the 'values' are the names)
I found it, problem fixed ;)
0

By "without result", I am assuming you are getting the else statement.

This is because using if nc[i] == '1' you are comparing class 'int' to class 'str' which will never match. Try the following code:

if nc[i] == 1:
            return 'ANTONIO' 

Alternatively, using dictionary(key-value) will be a better approach here.

Comments

0
def identificar_nombres(customer_names):
  customerlist = []
  for i in customer_names:
      if customer_names[i] == 1:
          customerlist.append("ANTONIO") 
      elif customer_names[i] == 2:
          customerlist.append("MARIA")
  return customerlist

nombres = identificar_nombres(customer_names) 
print(nombres)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.