title = ['james','tom','Kim'] # imagine this is a a file.txt
post_id = [5,6,9] #imagine this is a a file.txt
query = ['james','Kim'] # imagine this is a a file.txt
for i in query:
print(i)
for name, pid in zip(title, post_id):
if name == i:
print(pid)
break
else:
print('Not found')
Out Put
james
Kim
9
what im trying to do is i want to make a file.txt listed all post together with their post ID but i have a few item i want to check for a list of specific posts.
How to achieve like this bro?
What I’m Expected Output:
tom = 6
kim = 9
>Solution :
You can remove
for i in query:
print(i)
And remove zip(query, post_id):
and use for name in query:
to do comparing. By using this your code will loop through every value of query
and will compare it with title
.
So your final code should be:
title = ['james','tom','Kim']
post_id = [5,6,9]
query = ['james','Kim']
for name in query:
ids = post_id[title.index(name)]
if name in title:
print(f"{name} = {ids}")
else:
print('Not found')