基于参考字典值的查找并将新值附加到字典上
我想从参考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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可以使用字典理解:
You can use dictionary comprehension:
对于此类工作,我建议您查看
pandas
,这是一个数据分析python库,您也可以像类固醇和python一样考虑它。 https://pandas.pydata.org/ ,但没有它是可行的。一种可能的方法是将您的
fullTracks
列表转换为Artist_id - > URI
映射:那么产生您的
genRes_uris
映射是微不足道的: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 anartist_id -> uri
mapping:Then it is trivial to produce your
genres_uris
mapping:我尝试将其应用于“真实”完整轨道。我无法使结构工作。
FullTracks看起来像这样:
例如,我可以使用:
您对如何调整推荐的代码段的想法访问第一个列表项目的第一位艺术家?
I tried applying it to the "real" fulltracks. I can not get the structure working.
fulltracks looks like this:
For instance, I can access the first artist of the first list item using:
Do you have an idea on how to adjust your recommended code snippet?