1

Hey I'm fairly new to python, I'm currently studying Jython at University. So please forgive my ignorance.

Basically what I'm trying to do is create a "comic book" style picture using user selected images(easy enough) but because the number of pictures created is a variable, I've used a dict and assigned keys to each loop in the for function. But then I want to recall them again in another for loop. (also I'm aware theres possibly a few errors in my code, I'm just putting it up to give you an idea.

    def comicStrip():
      picture={}
      totalWidth=0
      totalHeight=0
      pixelCount=0
      loopCounter=0
      pictureCount=requestInteger("How many pictures do you want in the comic strip?(1-4)")
      while pictureCount <1 or pictureCount >4:     
        pictureCount=requestInteger("How many pictures do you want in the comic strip?(1-4)")
      for p in range(1,pictureCount+1):
        picture[p]=makePicture(pickAFile())
        width[p]=getWidth(picture[p])
        height[p]=getHeight[p]
        totalWidth=totalWidth+width[p]
        height=getHeight(picture[p])
        if height > totalHeight:
          totalHeight=height
        cStrip=makeEmptyPicture(totalWidth, totalHeight)
        while loopCounter < pictureCount:
          for targetX in range(0,p1Width):
           sourceY=0
           for targetY in range(0,p1Height):
             color = getColor(getPixel(picture1,sourceX,sourceY))
             setColor(getPixel(cStrip,targetX,targetY),color)
             sourceY=sourceY+1
           sourceX=sourceX+1
        addRectFilled(cStrip,0,0,p1Width,20,white)
        addRect(cStrip,0,0,p1Width,20)
        addRect(cStrip,0,0,p1Width,p1Height)
        caption=requestString("Enter the caption for this picture.") 
        addText(cStrip,1,19,str(caption))
4

1 回答 1

2

要打印 dict 键列表,有一个简单的命令:

d = {}
d.update({'a':1})
d.update({'b':2})
print d.keys()

给出一个输出

['a', 'b']

然后,要打印特定键的值,请使用以下行:

print d.get('a','')

其中'a'是关键。如果密钥不存在,则使用“.get”语法不会给出错误。

然后,您可以遍历所有键:

for element in d.keys():
    print d.get(element,'')

或者

for element in d.keys():
    print d[element]
于 2013-10-28T16:01:10.353 回答