Python 从dict自动调用键

Python 从dict自动调用键,python,dictionary,key,jython,Python,Dictionary,Key,Jython,嘿,我对python还相当陌生,我目前正在大学学习Jython。所以请原谅我的无知 基本上,我正在尝试使用用户选择的图像创建一个“漫画书”风格的图片(非常简单),但是因为创建的图片数量是一个变量,所以我使用了dict并为for函数中的每个循环分配了键。但我想在另一个for循环中再次调用它们。(另外,我知道我的代码中可能有一些错误,我只是想给你一个想法 def comicStrip(): picture={} totalWidth=0 totalHeig

嘿,我对python还相当陌生,我目前正在大学学习Jython。所以请原谅我的无知

基本上,我正在尝试使用用户选择的图像创建一个“漫画书”风格的图片(非常简单),但是因为创建的图片数量是一个变量,所以我使用了dict并为for函数中的每个循环分配了键。但我想在另一个for循环中再次调用它们。(另外,我知道我的代码中可能有一些错误,我只是想给你一个想法

    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))
def comicStrip():
图片={}
总宽度=0
总高度=0
像素计数=0
循环计数器=0
pictureCount=requestInteger(“您希望漫画中有多少张图片?(1-4)”)
而pictureCount 4:
pictureCount=requestInteger(“您希望漫画中有多少张图片?(1-4)”)
对于范围内的p(1,pictureCount+1):
picture[p]=makePicture(pickAFile())
宽度[p]=getWidth(图片[p])
高度[p]=getHeight[p]
总宽度=总宽度+宽度[p]
高度=获取高度(图片[p])
如果高度>总高度:
总高度=高度
cStrip=makeEmptyPicture(总宽度、总高度)
当loopCounter
要打印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,'')


你可能会发现发布到有用的反馈,一旦你得到工作。感谢堆,我一定会去看看。
d.update({“k”:“v”})
是一种非常复杂的方式来编写
d[“k”]=“v”
。我不知道
dict.update
的这种反模式用法是从哪里来的,但它真的开始让我感到不安…@Brunodesshuilliers,你对列表的.append()和.extend()有相同的感觉吗?我更喜欢字典的.update(),但这只是我的意见,伙计。
for element in d.keys():
    print d[element]