Home > Mobile >  Lookup and append new values to dictionary based on reference dictionary values
Lookup and append new values to dictionary based on reference dictionary values

Time:05-11

I want to lookup values ("uri") from a reference dict "fulltracks" and create a new dict with updated values. Each list is going to be used to create playlists with the spotify API afterwards.

Consider this list of dictionaries, each list item displays a track:

fulltracks = [{"artists":[1,2], "uri": "xyz",} 
              {"artists":[3], "uri": "abc"},
              {"artists":[4], "uri": "nmk"}, 
              {"artists":[5], "uri": "qwe"}, 

Additionally, I have this dictionary where the values are keys (artist) from fulltracks:

genres = {
  "rock": [1],
  "pop": [2],
  "hip hop": [3,4],
  "rap": [4],
  "house": [5]}

I would like to come out at an updated version of the dictionary "genres_uris":

genres_uris = {
  "rock": ["xyz"],
  "pop": ["xyz"],
  "hip hop": ["abc", "nmk"],
  "rap": ["abc"],
  "house": ["qwe"]}

I would call it a lookup in Excel but can not get my head into the right keywords for a Python approach/search. I came across pandas for this, is this library the right solution for my problem?

CodePudding user response:

You can use dictionary comprehension:

>>> {k: [d["uri"] for d in fulltracks for val in v if val in d["artists"]] for k, v in genres.items()}

{'rock': ['xyz'],
 'pop': ['xyz'],
 'hip hop': ['abc', 'nmk'],
 'rap': ['nmk'],
 'house': ['qwe']}

CodePudding user response:

For this kind of work, I would suggest you to check out pandas, which is a data analysis Python library, which you can also consider like Excel on steroids and in Python. https://pandas.pydata.org/

But it is doable without it. A possible way would be to turn your fulltracks list into an artist_id -> uri mapping:

artist_to_uri = {artist: fulltrack["uri"] for fulltrack in fulltracks for artist in fulltrack["artists"]}
# {1: 'xyz', 2: 'xyz', 3: 'abc', 4: 'nmk', 5: 'qwe'}

Then it is trivial to produce your genres_uris mapping:

{
genre: [artist_to_uri[artist] for artist in artists]
for genre, artists in genres.items()
}
# {'rock': ['xyz'],
# 'pop': ['xyz'],
# 'hip hop': ['abc', 'nmk'],
# 'rap': ['nmk'],
# 'house': ['qwe']}

CodePudding user response:

I tried applying it to the "real" fulltracks. I can not get the structure working.

fulltracks looks like this:

[Fulltracks = FullTrack with fields:
  album = SimpleAlbum(album_group, album_type, artists, ...)
  artists = [2 x SimpleArtist(external_urls, href, id, name, type, uri)
  uri = 8192118xq9101aß1],
[Fulltracks = FullTrack with fields:
  album = SimpleAlbum(album_group, album_type, artists, ...)
  artists = [3 x SimpleArtist(external_urls, href, id, name, type, uri)
  uri = 121212a0xaß1ß1010]

For instance, I can access the first artist of the first list item using:

fulltracks[0].artist[0].id

Do you have an idea on how to adjust your recommended code snippet?

  • Related