从不同列表中的数字查找项目在另一个列表中的位置

Finding the position of an item in another list from a number in a different list

根据我之前的问题进行开发 我想知道是否有一种方法可以在一个列表中包含一个元素并在另一个列表中找到该位置以及 return 该位置中的什么作为变量。例如:

list1 = [0,1,2,3,4]
list2 = ["0","hello","my","name","is","Daniel"]

如果用户输入数字 14,程序将 return "hello Daniel" 在计算机将 14 读取为 1 和 4 并在列表中查找位置的意义上。

我已经厌倦了使用 [list2.index(x) for x in list1] 希望它能起作用,但我一直无法更改代码以使其起作用。有什么简单的方法吗

我认为这会解决您的问题: 对于列表 1 中的 x: 打印列表 2[x]

你问的是这个吗?

你可以这样做:

input_num = 123
list1 = [0, 1, 2, 3, 4] # if list1 is not dervied from input number
list2 = ["0", "hello", "my", "name", "is", "Daniel"]
print(' '.join([list2[list1.index(int(ind))] for ind in str(input_num)]))

这将导致:

hello my name

此外,我建议您查看 https://docs.python.org/2/tutorial/datastructures.html 并尝试学习。