vendredi 31 janvier 2020

How to split a List of Dictionary into 2 seperates list of dictionaries with equality check

I am currently working on a Website which will detect the user games and the time. In my webapplication the user can set the games as finished (played). I successfully created a list of dictionary and the one game which got played to the end. I think a bit of Code illustration would help.

That is my code which should generate two seperate list of dictionaries. played_games_entries does actually work. I try it with an else but it doesn't work as I intendet. I know the problem but it is hard to explain and I dont now what my approach should be. If I am honest I am a bit confused because the if statement works and the else statement not.

def get_name_and_due(request, played_games, games_list):
    open_games_entries = []
    played_games_entries = []

    for game in games_list:
        for played_game in played_games:
            due_perc = convert_due_into_percentage(game['due'], request.session.get('max_due'))
            if game['game_name'] == played_game['played_game_name']:
                played_games_entries.append({'name': game['game_name'], 'due': due_perc})
                break
            else:
                pass

Here are my values for the parameters played_games and game_list.

games_list = [
    {'index': '1', 'game_name': 'Temtem', 'due': '00:30:04'},
    {'index': '2', 'game_name': 'The Forest', 'due': '10:00:30'},
    {'index': '3', 'game_name': 'The Witcher 3: Wild Hunt', 'due': '50:15:25'},
    {'index': '4', 'game_name': 'STAR WARS Jedi: Fallen Order', 'due': '18:15:00'}
]

played_games = [
    {'played_game_name': 'Grand Theft Auto: San Andreas'},
    {'played_game_name': 'The Witcher 3: Wild Hunt'},
    {'played_game_name': 'ONE PIECE: PIRATE WARRIORS 4'}
]

And the last code would be the expected result. And to point out played_game_entries does work

open_games_entries = [
    {'index': '1', 'game_name': 'Temtem', 'due': '24.44'},
    {'index': '2', 'game_name': 'The Forest', 'due': '10'},
    {'index': '4', 'game_name': 'STAR WARS Jedi: Fallen Order', 'due': '20'}
]

played_games_entries = [
    {'index': '3', 'game_name': 'The Witcher 3: Wild Hunt', 'due': '100'}
]

I worked all day to fix it on my own but I failed. I look the problem up on the internet and SO and many suggest Set. But didn't consider this approach because it is based on list and I have an List of Dictionaries. And I am really interested in seeing a approach with an for-loop because I really think it is possible.

Best Regards

Linda

Aucun commentaire:

Enregistrer un commentaire