Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/339.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/17.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,我正在尝试制作一个程序,用单词(也来自用户)替换一个列表(来自用户)中的数字。例如1=“c”,2=“z”,3=“p” 这就是我到目前为止所做的: wordlist = [None] numbers = [0] wordlist = input() numbers = input() wordlist.remove(None) numbers.remove(0) for numbers,item in enumerate(numbers): numbers.index(z) = wordl

我正在尝试制作一个程序,用单词(也来自用户)替换一个列表(来自用户)中的数字。例如1=“c”,2=“z”,3=“p”

这就是我到目前为止所做的:

wordlist = [None]
numbers = [0]
wordlist = input()
numbers = input()
wordlist.remove(None)
numbers.remove(0)
for numbers,item in enumerate(numbers):
    numbers.index(z) = wordlist.index
    z = z + 1
我不知道最后几行该怎么做,因为不管我怎么做,结果都没有达到预期效果

我试着按照以下思路做一些事情: 如果数字(z)等于wordlist.index,则用wordlist.index(p)替换数字(p)(p等于单词列表的索引数(只要单词列表中的所有单词都不同)和“数字”列表中的值数(例如数字=[1,2,3,1,4,5]那么z将是这个列表中的第一个和第四个值。)

如果只需要唯一的值,请使用set而不是list

================================

如果你想使用输入

wordlist = raw_input().split(' ')         #splits sentence into words using space as delimitor
numbers = [int(x) for x in raw_input().split()]    #int(x) is use for casting input string into integer type

ans=[]

for i in numbers:
    if i<len(wordlist):     #checks if value in numbers is below len of wordlist 
        ans.append(wordlist[i])

print(ans)
输出:

['orange', 'banana', 'orange']

numbers.index(z)
返回
z
的位置(此处未声明):完全错误。您已将其标记为python 3
input()
返回一个字符串,该字符串没有方法
remove
。您可能正在与python 2
input()
混合使用,后者读取输入并对其求值,然后返回结果。如果你能到达那里,你会在for循环中再次覆盖
数字。那
z
从哪里来?此时,您应该后退一步,再次阅读python教程,然后回到您的任务。谢谢您的帮助!但是,当我尝试将其应用于来自用户的列表时,该代码本身就可以工作,因为它只是说“无序类型”。我该如何解决这个问题?我该如何解决这个问题,使列表保持原来的顺序,因为单词的顺序与预期的不同
wordlist = raw_input().split(' ')         #splits sentence into words using space as delimitor
numbers = [int(x) for x in raw_input().split()]    #int(x) is use for casting input string into integer type

ans=[]

for i in numbers:
    if i<len(wordlist):     #checks if value in numbers is below len of wordlist 
        ans.append(wordlist[i])

print(ans)
apple orange banana
1 2 1
['orange', 'banana', 'orange']