从多个词典创建新列表

时间:2018-09-14 00:25:46

标签: python sorting dictionary

在下面的代码中,可以有多个FigureA.objects;这些由输出中的小写字母表示。每个对象都有其键/值(即shape:circle)的名称和特征字典。

    for objectsA in figureA.objects:
        thisObjectA = figureA.objects[objectsA]
        #print the objects lower-case letter (the shape's identifier)
        print objectsA

        for attributesA in thisObjectA.attributes:
            #print the attribute's for the object name:characteristic
            print attributesA, thisObjectA.attributes[attributesA]

输出为:

a
shape circle
fill no
size very large
b
shape plus
fill yes
angle 0
inside a
size small

如何代替打印,如何创建新的字符串列表或仅值的新列表?当然,不包括小写的a和b。 保持顺序(a的属性,然后是b的属性,等等)也很重要

非常感谢您的帮助!

1 个答案:

答案 0 :(得分:1)

在您的评论中,您需要以下内容:

 myList = []
 for objectsA in figureA.objects:
        thisObjectA = figureA.objects[objectsA]
        #print the objects lower-case letter (the shape's identifier)
        print objectsA

        for attributesA in thisObjectA.attributes:
            #print the attribute's for the object name:characteristic
            print attributesA, thisObjectA.attributes[attributesA]
            myList.append(attributesA + " " + thisObjectA.attributes[attributesA])

  myList.sort()

这应该创建所有属性的列表并对其进行排序