从多个词典创建新列表

2024-10-01 19:34:30 发布

您现在位置:Python中文网/ 问答频道 /正文

在下面的代码中,可以有多个图A.对象;这些由输出中的小写字母表示。这些对象中的每一个都有一个名称字典和它们的键/值的特征(即。形状:圆形). 你知道吗

    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的属性,等等)

非常感谢您的帮助!你知道吗


Tags: the对象inforsizeobjectsfillattributes
1条回答
网友
1楼 · 发布于 2024-10-01 19:34:30

从你的评论来看,你想要的是:

 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()

这将创建一个包含所有属性的列表并对它们进行排序

相关问题 更多 >

    热门问题