Help to compare two lists and get the number position
#1
Hello to all!
I need some help with this:

I have a main list with 50+ items.
And I have a second list that only contains some items from the first list.
What I want to do is compare the second list with the first, and with that, get the numerical position of the first list.

Example:

Main list:
Action, Adventure, Comedy, Horror, Drama, Comedy Romantic, Documentary, Espionage, Western, Fantasy, Science fiction, Mystery, Musical, Romance, Thriller.

Second list:
Horror, Western, Thriller
Horror is the 3th position, Western is the 9h, Thriller is the 15th position

So I want a third list, and that list would be (3, 9, 15).

And this is what I got so far:
Code:

    genre_list_path = xbmcvfs.translatePath('special://profile/addon_data/plugin.program.newsstand/db_genres')
    genre_list = os.listdir(genre_list_path) 
    old_genre_list_path = xbmcvfs.translatePath('special://profile/addon_data/plugin.program.newsstand/hellblazer')
    old_genre_list = os.listdir(old_genre_list_path) 

    if any(old_genre_list in genre_list for old_genre_list in genre_list):
        genre_position = ??
    else:
        genre_position = ''
        
    sDialog = xbmcgui.Dialog().multiselect('Select Genre', genre_list, preselect = genre_position)
    if sDialog is None: 
        xbmcgui.Dialog().notification(NewsStand, 'Genre not changed', xbmcgui.NOTIFICATION_INFO, 5000 )
        return False

    new_value_str = ', '.join([genre_list[x] for x in sDialog])
    xbmcgui.Dialog().notification(NewsStand, 'Genre is now {}'.format(new_value_str), xbmcgui.NOTIFICATION_INFO, 5000 )
    return True

Note: The main list is not absolute, the number of itens can change.

How do I make the genre_position variable transform the old_genre_list variable to (3, 9, 15)?

Thanks in advanced
Reply
#2
Responding my own question:

Got the answer here:

compare two lists in python and return indices of matched values

The this is the final code:

Code:

genre_list_path = xbmcvfs.translatePath('special://profile/addon_data/plugin.program.newsstand/db_genres')
genre_list = os.listdir(genre_list_path)
old_genre_list_path = xbmcvfs.translatePath('special://profile/addon_data/plugin.program.newsstand/hellblazer')
old_genre_list = os.listdir(old_genre_list_path)

if any(old_genre_list in genre_list for old_genre_list in genre_list):
     genre_position = [i for i, item in enumerate(genre_list) if item in old_genre_list]
else:
     genre_position = ''

sDialog = xbmcgui.Dialog().multiselect('Select Genre', genre_list, preselect = genre_position)
     if sDialog is None:
     xbmcgui.Dialog().notification(NewsStand, 'Genre not changed', xbmcgui.NOTIFICATION_INFO, 5000 )
     return False
new_value_str = ', '.join([genre_list[x] for x in sDialog])
xbmcgui.Dialog().notification(NewsStand, 'Genre is now {}'.format(new_value_str), xbmcgui.NOTIFICATION_INFO, 5000 )
return True
Reply

Logout Mark Read Team Forum Stats Members Help
Help to compare two lists and get the number position0