如何将整数数组与代码中的字符串数组相关联?

How to relate an array of integers to an array of strings in the code?

我是一个Python初学者,刚开始熟悉pymc库。在我的程序中,我生成了 1 到 100 之间的随机数。当我生成随机变量时,它显然是 returns 一个整数数组(在本例中是一个 10 整数数组):

customer_names = customer_names.random(size=10)

这个post的历史是我想通过一个table将每个int变量关联到一个特定的名称,好像每个名称都有一个标识符,但我不知道如何在代码中实现它。

我想要的是,当我 print(customer_names) 而不是获取数组 [ 54 2 45 75 22 19 16 34 67 88] 时,我想获取 [ Jose Maria Carmen Alejandro Davinia Eduardo Carlos Fátima Marc Mireia].

我尝试了以下方法:

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) 

但没有结果。

谢谢!

使用字典(键值对)存储这样的变量:

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

并像这样访问它:

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

输出:

['ANTONIO', 'MARIA CARMEN']

"without result",我假设您得到的是 else 语句。

这是因为使用 if nc[i] == '1' 你是在比较 class 'int'class 'str' 永远不会匹配。试试下面的代码:

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

或者,在这里使用字典(键值)将是更好的方法。

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)