0

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 1st 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)
3
  • 2
    What is the problem which you are facing? Commented Aug 19, 2018 at 14:08
  • @DavidZemens Yes thats the problem
    – Ali
    Commented Aug 19, 2018 at 14:16
  • Easier to iterate for lang in book_language_list and then get the index against the other list, as the ansewr below suggests. No need to do an indexed loop. Commented Aug 19, 2018 at 14:19

3 Answers 3

4

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.

1
  • The problem is some books has multiple languages and contains for example : 'eng', 'ita', 'fr' for just one item in the list, Because of this I have to to do text search using if any()
    – Ali
    Commented Aug 19, 2018 at 14:25
2

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
0

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]
0

Not the answer you're looking for? Browse other questions tagged or ask your own question.