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

2024/11/23 15:22:32

Developing on my previous question I'm wondering whether there is a way to have an element in a list and find that position in another list and return what is in the position as a variable. for example:

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

if the user enters the number 14 the program will return "hello Daniel" in the sense that the computer reads 14 as 1 and 4 and finds the position in list as an example.

I've tired using [list2.index(x) for x in list1] hoping that it would work but I've been unable to change the code so it does work. is there a simple way to do it

Answer

You can do something like this :

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)]))

This will result in :

hello my name

Also, i would suggest you to look at the https://docs.python.org/2/tutorial/datastructures.html and trying things out to learn.

https://en.xdnf.cn/q/120630.html

Related Q&A

Why am I getting array instead of vector size? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.Want to improve this question? Add details and clarify the problem by editing this post.Closed 3 years ago.Improve…

Sending Email attachment (.txt file) using Python 2.7 (smtplib) [duplicate]

This question already has answers here:How to send email attachments?(21 answers)Closed 9 years ago.So Im trying to send a .txt file as an attachment and I cant find the right code to work. Here is my…

Python selenium drop down menu click

i want to select option from a drop down menu, for this i use that :br.find_element_by_xpath("//*[@id=adyen-encrypted-form]/fieldset/div[3]/div[2]/div/div/div/div/div[2]/div/ul/li[5]/span").c…

TypeError: Argument must be rect style object - Pygame (Python [duplicate]

This question already has answers here:Closed 11 years ago.Possible Duplicate:Pygame (Python) - TypeError: Argument must be rect style object I am trying to make a brick breaker game in Pygame (with P…

How to compare dates in python and find the greater one

I want to compare 2 date and predict a label true if date 1 greater than date 2 and predict false date 1 less than date 2. I have trained the model but model is predicting wrong for near by dates that …

Python: is isalnum() the same as isalpha with isdigit?

Is there a way to concretely verify this? I tried to solve a coding question but it seems one of the test cases (not revealed to me) takes this as wrong. In what kinds of cases does this fail to be tr…

Python code works fine first time, but fails second time

The first time I run this block of code from Notebook it works fine:#Which letters and how many letters = ["a","b","c"] noOfLetters = len(letters)#Looking for all permutat…

How do I subtract a value in the dictionary? [closed]

Closed. This question needs debugging details. It is not currently accepting answers.Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to repro…

CSV IO python: converting a csv file into a list of lists

How to convert a csv file into a list of lists where each line is a list of entries with in a bigger list?Im having trouble with this because some of my entries have a comma in thema file like: 1,2,3…

Sheet of paper in millimeters [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.Want to improve this question? Add details and clarify the problem by editing this post.Closed 9 years ago.Improve…