Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How do i print specific parts of a users input based on another users input?

I’m confused on how I print a specific users input based on another input entered by a user. For example if I had a sample input of:

A A B B
1 3

I would want it to output A and B since A is the first element and B is the third.

How would I do this with a code like this:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

user = input().split()
user1 = input()
print()#Don't know what to do from here

Any help would be appreciated. Thank you.

>Solution :

Try using iteration to take values from user1 and get the values of user with indexes from user1.

So something like so:

values = input().split(" ") #changed user to values for readability
indexes = input().split(" ") #changed user1 to indexes

print(" ".join([values[int(i)-1] for i in indexes if int(i) <= len(values)]))

This uses list comprehension to simplify the iteration we need to use and join to convert the resultant list into a string.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading