0

In the code below, There can be multiple figureA.objects; these are represented by the lower case letters in the output. Each of these objects has a dictionary of names and characteristics for their key/values (i.e. 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]

The output is:

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

Instead of printing, how do I create either a new list of strings or a new list of only the values? Lower-case a and b won't be included, of course. It's also important the order is maintained (a's attributes, then b's, etc.)

Any help is much appreciated!

yodish
  • 733
  • 2
  • 10
  • 28
  • Possible duplicate of [How to merge two dictionaries in a single expression?](https://stackoverflow.com/questions/38987/how-to-merge-two-dictionaries-in-a-single-expression) – Mars Sep 14 '18 at 00:28
  • I looked at that post and i'm not sure it helps me much here. What i'm hoping to do is load new key:values into a new dictionary in the second loop. I think the subsequent dictionaries that get read in that loop should be able to go into the new dictionary the same as the print statement prints them out; but, i'm just not sure. – yodish Sep 14 '18 at 01:18
  • Can you post an example of what you're looking for for output? – Mars Sep 14 '18 at 01:35
  • Essentially, it would be the same output as above (minus the 'a' and 'b'). However, it would all be in 1 dictionary. – yodish Sep 14 '18 at 01:39
  • you have multiple copies of the same key, so thats not possible – Mars Sep 14 '18 at 01:48
  • if you say myDict["shape"] = "circle" and then say myDict["shape"] = plus, then myDict["shape"] becomes "plus". If that is what you want, then the link above should give you what you want – Mars Sep 14 '18 at 01:51
  • I appreciate the info. I think maybe what I want is a list of strings then? I hadn't considered that I cant have the same key twice in a dictionary. Or, possibly just a new list of the values only. – yodish Sep 14 '18 at 01:58
  • Got it. Give me a sec. Also, please update your question to reflect that! – Mars Sep 14 '18 at 02:00

1 Answers1

1

From your comments, you want something like:

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

This should create a list of all of your attributes and sort them

Mars
  • 2,505
  • 17
  • 26