How do i print specific parts of a users input based on another users input?
Question:
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:
user = input().split()
user1 = input()
print()#Don't know what to do from here
Any help would be appreciated. Thank you.
Answers:
Maybe this is the code you are looking for:
user = input().split()
user1 = input().split()
for i in range(len(user)):
if str(i + 1) in user1:
print(user[i])
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.
this will solve your problem
user = input().split()
user1 = [int(i) for i in input().split()]
for i in user1:
print(user[i])
> A A B B
> 1 3
I would assign the input values to lists, then use the numbers in the second list as indices
if __name__ == '__main__':
first_input = input('Input some letters: ')
first_list = first_input.split(' ')
second_input = input('Input some numbers: ')
second_list = second_input.split(' ')
for num in second_list:
second_list[second_list.index(num)] = int(num)
third_list = []
for num in second_list:
third_list.append(first_list[num])
print(f'The letters are: {third_list}')
And the output would be:
> Input some letters: A A B B
> Input some numbers: 1 3
> The letters are: ['A', 'B']
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:
user = input().split()
user1 = input()
print()#Don't know what to do from here
Any help would be appreciated. Thank you.
Maybe this is the code you are looking for:
user = input().split()
user1 = input().split()
for i in range(len(user)):
if str(i + 1) in user1:
print(user[i])
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.
this will solve your problem
user = input().split()
user1 = [int(i) for i in input().split()]
for i in user1:
print(user[i])
> A A B B
> 1 3
I would assign the input values to lists, then use the numbers in the second list as indices
if __name__ == '__main__':
first_input = input('Input some letters: ')
first_list = first_input.split(' ')
second_input = input('Input some numbers: ')
second_list = second_input.split(' ')
for num in second_list:
second_list[second_list.index(num)] = int(num)
third_list = []
for num in second_list:
third_list.append(first_list[num])
print(f'The letters are: {third_list}')
And the output would be:
> Input some letters: A A B B
> Input some numbers: 1 3
> The letters are: ['A', 'B']