Reputation: 339
I have two list.
The first list contains languages and contains about 30000
items.
['es','de', 'ita', ....]
The second list contains about twenty language codes, as follow :
['eng', 'ar', 'fr', 'ch', 'jp', 'ita' .... ]
What I want to do is to search and see if the 1
st book is related to the code 19
( 1 is the index of the book in the first list) and if found, print the code index from the second list
import csv
book_language_list = []
with open('language_table_for_search.csv', 'r') as rf:
reader = csv.reader(rf, delimiter=',')
for row in reader:
book_language_list.append(row[2])
book_language_distinc = []
with open('language_codes.csv', 'r') as rf:
reader = csv.reader(rf, delimiter=',')
for row in reader:
book_language_distinc.append(row[1])
for i in range(0, len(book_language_list)):
if any(book_language_list[i] in s for s in book_language_distinc):
# print the book id=i has language=(found language index)
Upvotes: 0
Views: 154
Reputation: 56
Simple Solution:
Use set()
set & set
returns intersection of two sets.
example:
books = ['es', 'de', 'ita', 'eng']
codes = ['eng', 'ar', 'fr', 'ch', 'jp', 'ita']
for r in set(books)&set(codes):
print(r, codes.index(r))
returns:
eng 0
ita 5
Upvotes: 2
Reputation: 339
The problem solved as follow, using enumerate
:
for i in range(0, len(book_language_list)):
print("book {} has language {}={}".format(i, book_language_list[i], [j for j, s in enumerate(book_language_distinc) if book_language_list[i] in s]))
output :
book 27279 has language 'us'=[5]
Upvotes: 0
Reputation: 61910
One approach is to use the index function, from the documentation:
This method returns index of the found object otherwise raise an exception indicating that value does not find.
Like this:
books = ['es', 'de', 'ita', 'eng']
codes = ['eng', 'ar', 'fr', 'ch', 'jp', 'ita']
for book in books:
try:
ii = codes.index(book)
print(book, ii)
except ValueError:
pass
Output:
ita 5
eng 0
If you have multiple languages by book and that each book is represented by a list, you can do it like this:
books = [['es'], ['de'], ['ita', 'ch'], ['eng']]
codes = {code: ii for ii, code in enumerate(['eng', 'ar', 'fr', 'ch', 'jp', 'ita'])}
for book in books:
book_codes = [(lang, codes[lang]) for lang in book if lang in codes]
if book_codes:
print(book_codes)
Output:
[('ita', 5), ('ch', 3)]
[('eng', 0)]
The line:
codes = {code: ii for ii, code in enumerate(['eng', 'ar', 'fr', 'ch', 'jp', 'ita'])}
creates a dictionary where the keys are the language codes and the values the index on the list. Finally,
book_codes = [(lang, codes[lang]) for lang in book if lang in codes]
get the languages of the book that are in codes, if the list is not empty the result is printed.
Upvotes: 4