基于参考字典值的查找并将新值附加到字典上

发布于 2025-01-28 05:26:28 字数 788 浏览 3 评论 0原文

我想从参考dict“ fullTracks”中查找值(“ URI”),并创建具有更新值的新dict。之后,每个列表将用于使用Spotify API创建播放列表。

考虑此词典列表,每个列表项目都会显示一个曲目:

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

此外,我还有这个词典,其中的值是fulltracks的键(艺术家):

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

我想在词典“ genres_uris”的更新版本中出来:

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

我会打电话给我 。它是Excel中的查找,但无法让我进入正确的关键字,以进行Python方法/搜索。为此,我遇到了大熊猫,这个库是我问题的正确解决方案吗?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

九厘米的零° 2025-02-04 05:26:28

您可以使用字典理解:

>>> {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']}

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']}
鸠魁 2025-02-04 05:26:28

对于此类工作,我建议您查看pandas,这是一个数据分析python库,您也可以像类固醇和python一样考虑它。 https://pandas.pydata.org/

但没有它是可行的。一种可能的方法是将您的fullTracks列表转换为Artist_id - > URI映射:

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'}

那么产生您的genRes_uris映射是微不足道的:

{
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']}

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']}
旧伤还要旧人安 2025-02-04 05:26:28

我尝试将其应用于“真实”完整轨道。我无法使结构工作。

FullTracks看起来像这样:

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

例如,我可以使用:

fulltracks[0].artist[0].id

您对如何调整推荐的代码段的想法访问第一个列表项目的第一位艺术家?

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?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文