0

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')?

5
  • 1
    do you want what_to_look to be a list (if it contains values found in title)? Commented Jan 6, 2023 at 18:47
  • YES bro exactly correct Commented Jan 6, 2023 at 18:49
  • In an input, you just get one single line from STDIN. If you are sure of the number of lines to input and use altogether, better input those many lines (say 2 in your case) and combine the inputs into a tuple. Check out Tuple Comprehension or List Comprehension for some one-liner syntaxes for it. Just in case you want to have multi-element input instead, use a delimiter such as a space-separated line of input, and convert it to list of individual elements using .split() method, in your case input().split() Commented Jan 6, 2023 at 18:50
  • Please read How to Ask: Research required. Did you search? Found the answer here on SO. Commented Jan 6, 2023 at 19:25
  • Does this answer your question? Passing Inputs into a list in python Commented Jan 6, 2023 at 19:34

5 Answers 5

1

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!

Sign up to request clarification or add additional context in comments.

Comments

1

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.

2 Comments

Minimal complete verifiable example: split and then loop over the list items to find each.
"split the string value" - by what delimiter? A short example for input and output would help - also the user to enter names correctly input('Enter one or multiple names separated by :').
1

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

6 Comments

@LegendCoder What is "BRO" - you can also address people by name and thank them for their help. For the "input to a list of text" you could read all the answers you got - consider an upvote to say "Thank You".
@LegendCoder what_to_look is a list of text. You can print it. ['james', 'tom'].split() is invalid. If you want check everything by character, you could use list(what_to_look)
For 1 per line or reading from text file, you can ask in new question with detail of information you want to achieve.
@LegendCoder fixed... you can try james kim = 5, 9
@LegendCoder Please take the tour and read How to Ask. Clarify not here with comments, but edit your question and add them there (like I just did). One question per post, research first. Ask thefile.txt input in another question.
|
1

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

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.