I am trying to make a program that replaces numbers in one list (from the user) with words (also from the user). For example 1 = "c", 2 = "z", 3 = "p".
This is what I have done so far:
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
I`m not sure on what to do on the last couple of lines as no matter what I do it ends up just not working as intended.
I`m trying to do something along the lines of: if numbers(z) is equal to wordlist.index then replace numbers(p) with wordlist.index(p) (with p being equal to the number of the index of the list of words (as long as all the words In the wordslist are different) and the number of the values in the "numbers"list (for example numbers = [1,2,3,1,4,5] then z would be the first and fourth value in this list))
wordlist=['apple','orange','banana']
numbers=[1,2,1]
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)
Output :
['orange', 'banana', 'orange']
If you want only unique values then instead of using list,use set
================================
If you want to take use input
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)
Input:
apple orange banana
1 2 1
Output:
['orange', 'banana', 'orange']
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.