简体   繁体   中英

How to get an item index in a list?

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)

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.

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

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]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM