将字典键的值赋给变量

时间:2015-06-14 19:02:37

标签: python

我在Python中有以下代码:

def buildXmlUpdate(dfrom, roomId, ldays):   

    start_date_sard.text = dfrom
    roomId = str(roomId) 
    room_id_sard.text = roomId
    ldays = {'avail': str(), 'price': str()}
    availability_in_data.text = ldays['avail']
    price_in_data.text = ldays['price']

    for n in ldays:
        print (dfrom, roomId, ldays)

现在正在运行

buildXmlUpdate ('21/12/2015', 1, [{'avail': 1, 'price': 100}, {'avail': 3, 'price': 120}])

我得到以下输出

('21/12/2015', '1', {'avail': '', 'price': ''})
('21/12/2015', '1', {'avail': '', 'price': ''})

换句话说:

('21/12/2015', '1', {'avail': 1, 'price': 100})
('21/12/2015', '1', {'avail': 3, 'price': 120})

如您所见,字典availprice键设置为空字符串,但我想根据方法中的ldays参数设置它们。

我做错了什么?

1 个答案:

答案 0 :(得分:0)

解决:

def buildXmlUpdate(dfrom, roomId, ldays):   

start_date_sard.text = dfrom
roomId = str(roomId)  
room_id_sard.text = roomId
#ldays = {'avail': str(), 'price': str()}
#availability_in_data.text = ldays['avail']
#price_in_data.text = ldays['price']


for n in ldays:
    print (dfrom, roomId, n)
    #availability_in_data.text = get.ldays['avail']
    #price_in_data.txt = get.ldays['price']
    ldays[-1]['avail'] = str(ldays[-1]['avail'])
    ldays[-1]['price'] =str(ldays[-1]['price'])
    availability_in_data.text = ldays[-1]['avail']
    price_in_data.text = ldays[-1]['price']

谢谢大家!