将字典合并为列表问题-python [英] Merging dictionary into list issues - python

查看:48
本文介绍了将字典合并为列表问题-python的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问题

我想将字典合并到使用For循环生成的列表的末尾.似乎可行,但问题在于大括号留在字典中而不是与列表合并.有点难以解释,因此这里有两个示例:

I want to merge a dictionary onto the end of a list that I am generating using a For loop. It seems to work but the problem, is that the curly braces stay around the dictionary instead of merging with the list. Its kinda hard to explain so here are two examples:

这就是我想要的(抱歉,您必须向右滚动以查看 path 键值):

This is what I want (sorry, you have to scroll to the right to see the path key value):

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

当我希望将其作为 disc

这是我的相关代码:

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)

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

Any ideas or suggestions would be appreciated

推荐答案

您应该在列表内更新字典,而不是附加到列表的 tag_dicts .因此,而不是下面的内容:

Instead of appending to the tag_dicts which is a list, you should update your dictionary inside the list. So instead of below:

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

使用以下内容更新字典:

use the below to update the dictionary:

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

这篇关于将字典合并为列表问题-python的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆