【问题标题】:Simplify PyreResponse iteration简化 PyreResponse 迭代
【发布时间】:2018-01-16 04:17:19
【问题描述】:

这是我的代码:

lastRow = db.child("twitter-search").order_by_key().limit_to_last(1).get(user['idToken'])

latestId = None

for row in lastRow.each():
for key, value in row.val().items():
    if key == "latest_id":
        latestId = value

它正在工作,但有没有办法让它更短?

【问题讨论】:

  • 您可以指定为: latestId = row.val()["latest_id"] 而不是每次都遍历行

标签: python loops dictionary iteration


【解决方案1】:

O(1) 检查密钥是否存在,如果存在则更新latestId

latestId = None
for row in lastRow.each():
    if "latest_id" in row:
        latestId = row["latest_id"]

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-04-20
    • 1970-01-01
    • 1970-01-01
    • 2019-09-15
    • 1970-01-01
    • 1970-01-01
    • 2020-09-30
    • 1970-01-01
    相关资源
    最近更新 更多