Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/bash/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何将数字转换为该程序中指定的单词?_Python_Python 3.x - Fatal编程技术网

Python 如何将数字转换为该程序中指定的单词?

Python 如何将数字转换为该程序中指定的单词?,python,python-3.x,Python,Python 3.x,这是一个程序,在这个程序中,数字列表必须根据在列表中的位置转换为单词列表。例如: numbers = [1, 2, 1, 3, 2, 4, 1 ,5] #Input from user this is an example words = ["apple", "pear", "lemon", "grape", "pineapple"] 数字列表中的数字是单词列表中每个项目的索引。 此场景的输出应为: apple pear apple lemon pear grape apple pineap

这是一个程序,在这个程序中,数字列表必须根据在列表中的位置转换为单词列表。例如:

numbers = [1, 2, 1, 3, 2, 4, 1 ,5] #Input from user this is an example
words = ["apple", "pear", "lemon", "grape", "pineapple"] 
数字列表中的数字是单词列表中每个项目的索引。 此场景的输出应为:

apple pear apple lemon pear grape apple pineapple
记住,这只是一个例子;这些列表将取自文本文件

以下是我一直坚持的部分:

for i in numbers: #i for the loop
    match = re.match(words.index[i], numbers)
    if match:
        numbers = words #I have no clue here
print(numbers)

这只是我的程序的一个摘录,需要对它做一些工作,以便变量是正确的。

您需要遍历数字列表。现在,请使用For循环:

在该循环中,您需要找到与该索引匹配的单词:

    word = words[index-1]
。。。然后简单地打印单词

先进的

有很多方法可以把它放在一行中;我相信其他人会知道如何列出理解和连接或映射操作,例如下面的代码。现在选择你自己的舒适度

print ' '.join([words[index-1] for index in numbers])
这更像是蟒蛇。。。但要按照自己的节奏学习。

使用映射而不是不必要的正则表达式:

>>> numbers = [1, 2, 1, 3, 2, 4, 1 ,5] #Input from user this is an example
>>> words = ["apple", "pear", "lemon", "grape", "pineapple"]
>>> list(map(lambda x: words[x - 1], numbers))
['apple', 'pear', 'apple', 'lemon', 'pear', 'grape', 'apple', 'pineapple']
用人类的话来说,这意味着,将数组中的每个数字映射到其索引中的单词数小于-1的位置,以获得基于0的索引

您可以使用

>>> [words[index - 1] for index in numbers]
['apple', 'pear', 'apple', 'lemon', 'pear', 'grape', 'apple', 'pineapple']

非常先进,可能比以往任何时候都更强大:

import numpy as np
words = np.array(["apple", "pear", "lemon", "grape", "pineapple"])
numbers = np.array([1, 2, 1, 3, 2, 4, 1 ,5])
words[numbers - 1].tolist()
# ['apple', 'pear', 'apple', 'lemon', 'pear', 'grape', 'apple', 'pineapple']

可能是index-1出于某种原因,它只是出现了一个类型错误:TypeError:-:“str”和“int”的操作数类型不受支持。那么您的索引数组可能是字符串,而不是从问题中看到的字符串。第二个例子请尝试单词[intx-1]索引而不是x谢谢!你真是个救命恩人。这一部分我已经讲了好几个小时了。我自己也没有意识到这一点,这让我很惊讶。最后一个问题。如何将其以原始形式显示给用户。我的意思是,当我在这个语句周围使用print函数时,它如何显示[apple,pear,apple,etc],我如何在不删除标点符号和特殊字符的情况下像apple,pear,apple等那样显示它。
import numpy as np
words = np.array(["apple", "pear", "lemon", "grape", "pineapple"])
numbers = np.array([1, 2, 1, 3, 2, 4, 1 ,5])
words[numbers - 1].tolist()
# ['apple', 'pear', 'apple', 'lemon', 'pear', 'grape', 'apple', 'pineapple']
  numbers = [1, 2, 1, 3, 2, 4, 1 ,5]  # Input from user. This is an example
  words = ["apple", "pear", "lemon", "grape", "pineapple"] 

  for i in range(len(numbers)):
      numbers[i] = words[numbers[i] - 1]

  print numbers