Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/list/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
从字典创建列表-python_Python_List_Dictionary_Set - Fatal编程技术网

从字典创建列表-python

从字典创建列表-python,python,list,dictionary,set,Python,List,Dictionary,Set,我有以下xml文件,其中包含重复的groupId。我想把它转换成一个接受这个多ID的字典 到目前为止,当我尝试将列表转换为字典时,它消除了所有键(但最后一个键除外) 所以我想把我的字典转换成一个列表。 有什么帮助吗 XML是: <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>3.8.1</

我有以下xml文件,其中包含重复的groupId。我想把它转换成一个接受这个多ID的字典

到目前为止,当我尝试将列表转换为字典时,它消除了所有键(但最后一个键除外)

所以我想把我的字典转换成一个列表。 有什么帮助吗

XML是:

<dependency>
    <groupId>junit</groupId>
    <artifactId>junit</artifactId>
    <version>3.8.1</version>
    <scope>test</scope>
</dependency>
<dependency>
    <groupId>org.hibernate</groupId>
    <artifactId>hibernate-core</artifactId>
    <version>3.6.3.Final</version>
</dependency>
<dependency>
    <groupId>org.hibernate</groupId>
    <artifactId>hibernate</artifactId>
    <version>3.2.5.ga</version>
</dependency>
<dependency>
    <groupId>org.hibernate</groupId>
    <artifactId>hibernate-entitymanager</artifactId>
    <version>3.3.2.GA</version>
</dependency>
输出

defaultdict(<class 'list'>, {'junit': [{'artifactId': 'junit', 'version': '3.8.1'}], 'org.hibernate': [{'artifactId': 'hibernate-core', 'version': '3.6.3.Final'}})
defaultdict(,{'junit':[{'artifactId':'junit','version':'3.8.1'}],'org.hibernate':[{'artifactId':'hibernate core','version':'3.6.3.Final'})
预期产出:

defaultdict(<class 'list'>, {'junit': [{'artifactId': 'junit', 'version': '3.8.1'}], 'org.hibernate': [{'artifactId': 'hibernate-core', 'version': '3.6.3.Final'}, 'org.hibernate': [{'artifactId': 'hibernate', 'version': '3.2.5.ga'}, 'org.hibernate': [{'artifactId': 'hibernate-entitymanager', 'version': '3.3.2.GA'}})
defaultdict(,{'junit':[{'artifactId':'junit','version':'3.8.1','org.hibernate':[{'artifactId':'hibernate core','version':'3.6.3.Final','org.hibernate':[{'artifactId':'hibernate','version':'3.2.5.ga','org.hibernate':[{'artifactId':'e entitymanager','version':'3.3.2.2.ga'})

根据我的研究,字典不能有重复的值,所以我需要把它放在一个列表或集合中。

我不太清楚你想做什么,但我认为你的问题是你在每次迭代中都定义了你的列表(infoList)


infoList=[]
移出循环以解决该问题。

我认为缩进是错误的,请尝试以下操作:

depend = root.xpath("//*[local-name()='dependency']")
dependencyInfo = defaultdict(dict)

for dep in depend:
    infoList = []
    self.counter += 1
    for child in dep.getchildren():
        infoList.append(child.tag.split('}')[1])
        infoList.append(child.text)

    # this is inside the for loop
    dependencyInfo[infoList[1]].update({infoList[2]: infoList[3], infoList[4]: infoList[5]})
您需要
dependencyInfo
来更改内的for”循环


否则,它将使用上次迭代的信息进行更新


你能提供一个关于最终字典<代码>依赖关系信息>代码的例子吗?更新的@丹尼尔预期输出包含密钥<代码> org .Hibernate < /Cord>多次(因为在XML中代码< GROPID/<代码>多次包含相同的文本)。你应该考虑不同的结构(列表和DICT的层次结构)对于最终输出。对不起,编辑了我的代码。当我放入stackoverflow时,它弄乱了缩进。
depend = root.xpath("//*[local-name()='dependency']")
dependencyInfo = defaultdict(dict)

for dep in depend:
    infoList = []
    self.counter += 1
    for child in dep.getchildren():
        infoList.append(child.tag.split('}')[1])
        infoList.append(child.text)

    # this is inside the for loop
    dependencyInfo[infoList[1]].update({infoList[2]: infoList[3], infoList[4]: infoList[5]})
import xmltodict
from collections import defaultdict

results = defaultdict(list)

with open("pom_file_path>") as f:
    parse_ = xmltodict.parse(f.read()).get('project', {})

    for d in parse_.get("dependencies", {}).get("dependency", []):
        results[d['groupId']].append(
            {"artifactId": d['artifactId'], 'version': d['version']}
        )