Code
I want to input multiple values (see commented line below), so the user can easily search for multiple values and loop can find the values.
How can I change the input to a lists of text, so that the program check everything?
Like this: what_to_look = ['james','tom'].split()
How about 1 per line – when reading it from a file.txt
?
I think I am getting a wrong result, post_id
is actual id of post URL.
I want to know if and what is the id of specific post py listing their name in file.txt
.
With that said, "james kim"
should give answers: 5 and 9 but not a 5 and 6.
title = ['james','tom','kim']
post_ids = [5,6,9]
what_to_look = input('Enter Name: ') # convert this like (what_to_look = ['james','tom'] )
# search for the values
if what_to_look in title:
position = title.index(what_to_look)
post_id = post_ids[position]
print('found', post_id)
else:
print('not found')
How can I convert the input into a list like ('james','kim')
?
CodePudding user response:
I think you want to take a list of values as input you can input it using the below code, for example
what_to_look = input().split()
then you can enter the values as space separated text in single line. Hope that help!
CodePudding user response:
You can achieve it through split():
title = ['james','tom','kim']
post_ids = [5,6,9]
what_to_look = input('Enter Name: ')
You can split the string value into a list;
entered_list = what_to_look.split()
This will create a list from the entered string.
Edit: It's recommended that you use a delimiter for the entered value as suggested by @hc_dev. This will create a better user experience and lead to better search results.
CodePudding user response:
Use split()
and iterate splitted what_to_look
input and get its index if it in the title
list:
title = ['james','tom','kim']
post_ids = [5,6,9]
what_to_look = input('Enter Name: ').split()
for name in what_to_look:
if name in title:
post_id = post_ids[title.index(name)]
print('Found', post_id)
else:
print('Not found')
Output:
Enter Name: james tom
Found 5
Found 6
Enter Name: tom jerry
Found 5
Not found
CodePudding user response:
Python strings provide the .split
function which breaks it into a list according to the specified separator.
For instance, names = what_to_look.split(" ")
will break the what_to_look
at each spaces.
For further reference, here is a link to the doc split
CodePudding user response:
Here is a demo on IdeOne to try:
titles = ['james','tom','kim']
locations = [5,6,9]
find_str = input('Find Names (separated by space): ')
terms = find_str.split() # split entered string by delimiter space into a list
print('Searching for terms:', terms)
for t in terms: # loop through each given name and search it in titles
if t.lower() in titles: # compare lower-case, if found inside titles
i = titles.index(t.lower()) # find position which is index for other list
loc = locations[i] # use found position to get location number or URL
print("Found given term:", t, "on index:", i, " has location:", loc)
My input (3 terms separated by space):
James t. kirk
Console output (incl. entered line):
Find Names (separated by space): James t. kirk
Searching for terms: ['James', 't.', 'kirk']
Found given term: James on index: 0 has location: 5