-3

我有两个字典:

第一个是 legenddict :

legenddict = [ 'Standard_Animator: blue, 3f7fff, 00bfff, 3fffbf, green, bfff3f, ffbf00, ff7f00, red' ,
               'Extended_Animator:    LightGray, blue,3f7fff,00bfff,3fffbf,green,bfff3f,ffbf00,ff7f00,red, Magenta' ]

第二个是 colordict :

colordict = ['blue' : 'ff00ff', 'red' = '808080', 'lightgray':'d3d3d3','magneta':'00ff00']

我想打印 legenddict 的所有值以与 colordict 的键进行比较它应该产生输出为所有十六进制数字的颜色

4

1 回答 1

2

Well, first off your syntax for defining literal dictionaries is incorrect. Dictionaries are surrounded by curly brackets like this: {} instead of square brackets like this: [] If you want 'Standard_Animator' and 'Extended_Animator' to be keys for lists of colors, you would want to do something like this:

legenddict = {"Standard_Animator" : ["blue", 3f7fff, 00bfff, 3fffbf, "green", bfff3f, ffbf00, ff7f00, "red"], 
              "Extended_Animator" : ["lightgray", "blue", 3f7fff, 00bfff, 3fffbf, "green",  bfff3f, ffbf00, ff7f00, "red", "magenta"}

colordict = {'blue':'ff00ff', 'red':'808080', 'lightgray':'d3d3d3', 'magenta':'00ff00'}

So, to print the values in legenddict using the color names in colordict, you can check to see if the colors are keys in colordict, and if so, look up the values:

for color in legenddict['Standard_Animator']:
    if color in colordict:
        print colordict[color]
    else:
        print color


for color in legenddict['Extended_Animator']:
    if color in colordict:
        print colordict[color]
    else:
        print color
于 2013-09-04T10:24:17.490 回答