【问题标题】:Merging dictionary into list issues - python将字典合并到列表问题中 - python
【发布时间】:2021-03-28 14:20:56
【问题描述】:

问题

我想将字典合并到我使用 For 循环生成的列表的末尾。它似乎有效,但问题是花括号留在字典周围而不是与列表合并。这有点难以解释,所以这里有两个例子:

这就是我想要的(抱歉,你必须向右滚动才能看到path键值):

[{'comment': u'Fighter', 'album': u'Dead or Alive2', 'audio_offset': None, 
'title': u'You Are Under My Control ~Beautiful Version00~', 'track': None, 
'disc_total': None, 'artist': u'Makoto Hosoi', 'track_total': None, 'channels': 2, 'genre': None, 'albumartist': u'Makoto Hosoi', 'filesize': 669796508L, 
'composer': u'Makoto Hosoi', 'year': u'1999', 'duration': 403.26953333333336, 'samplerate': 48000, 'bitrate': 294651.393, 'disc': None, 
'path': '\\\\Vgmstation\\\\e\\\\Dreamcast\\\\Dead or Alive 2\\You Are Under My Control ~Beautiful Version00~.mp4'}]

这是正在发生的事情:

[{'comment': u'Fighter', 'album': u'Dead or Alive2', 'audio_offset': None, 'title': u'You Are Under My Control ~Beautiful Version00~', 'track': None, 'disc_total': None, 'artist': u'Makoto Hosoi', 'track_total': None, 'channels': 2, 'genre': None, 'albumartist': u'Makoto Hosoi', 'filesize': 669796508L, 'composer': u'Makoto Hosoi', 'year': u'1999',
 'duration': 403.26953333333336, 'samplerate': 48000, 'bitrate': 294651.393, 'disc': None}, 
{'path': '\\\\Vgmstation\\\\e\\\\Dreamcast\\\\Dead or Alive 2\\You Are Under My Control ~Beautiful Version00~.mp4'}]

当我希望 path 作为 disc 之后的最后一个键值时,请注意大括号是如何围绕在 path 键上的

这是我的相关代码:

import os
import subprocess
from tinytag import TinyTag
import json

tag = ''
tag_dicts = []
extf = ['$RECYCLE.BIN']
for root, dirs, files in os.walk(r'\\Vgmstation\\e\\Dreamcast\\Dead or Alive 2', followlinks=True):
    dirs[:] = [d for d in dirs if d not in extf]
    for name in files:
        if name.endswith(".mp4"):
            musiclist=str(os.path.join(root, name))
            tag = TinyTag.get(musiclist)
            musicpath_dict = {'path': os.path.join(root,name)}
            musiccopy = musicpath_dict.copy()
            tag_dicts.append(tag.as_dict())
            tag_dicts.append(musiccopy)


print(tag_dicts)

任何想法或建议将不胜感激

【问题讨论】:

  • 请提供预期的MRE - Minimal, Reproducible Example。显示中间结果与预期结果的偏差。我们应该能够将您的代码块粘贴到文件中,运行它并重现您的问题。这也让我们可以在您的上下文中测试任何建议。我们期望这个问题的最小工作示例,包括跟踪内部操作的适当代码。您发布的代码无法运行,因为它依赖于私有文件。
  • 如果你希望它是一本字典,你应该使用dict.update
  • 这段代码远非最小。当您的帖子强制水平滚动时,您会失去读者。修复格式以适合页面。更好的是,缩短示例。
  • 乍一看,问题似乎是你appended 了一些你想成为update 的东西。阅读文档以了解差异,或者只是通过有关 dicts 及其方法的教程。

标签: python list dictionary


【解决方案1】:

您应该更新列表中的字典,而不是附加到列表中的tag_dicts。所以而不是下面:

tag_dicts.append(tag.as_dict())
tag_dicts.append(musiccopy)

使用以下内容更新字典:

tag_dicts.append(tag.as_dict())
tag_dicts[0].update(musiccopy)

【讨论】:

  • 我尝试了tag_dicts.append((tag.as_dict()).update(musiccopy))tag_dicts.append((tag.as_dict()).update(musicpath_dict)) 但都返回:[None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None] 我将尝试做一些更改,看看是否可以使用更新方法让它工作。虽然,我不确定这是否会起作用,因为tag_dicts 中没有path。只有musiccopy 可以。谢谢领导!!
  • @JatonJameerJustice - 我已经更新了我的答案,看看它
猜你喜欢
  • 2017-04-11
  • 1970-01-01
  • 2013-02-19
  • 2012-08-11
  • 2012-07-17
  • 2020-10-12
  • 2013-12-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多