Python - 我需要这个键,在for循环之外访问的值。我怎样才能使这个全球化

时间:2018-02-26 20:30:11

标签: python dictionary

这是我的示例代码:     for key,application_list.items()中的值:     其中application_list是JSON。 我需要这个键和值,稍后超出for循环的范围,如下所示:

for key,value in application_list.items():
  .....
  ......
  more computations
dt = datetime.fromtimestamp(int(key))

我怎样才能实现这个目标?

1 个答案:

答案 0 :(得分:0)

如果您要存储特定密钥供以后使用,只需指定一个变量,例如: x,如下所示:

for key, value in application_list.items():

    # computations

    x = key  # for your chosen key dependent on your conditions

dt = datetime.fromtimestamp(int(x))

如果您只想保存第一个键:

for idx, (key, value) in enumerate(application_list.items(), 1):

    # computations

    if idx == 1:
        x = key  # for your chosen key dependent on your conditions

dt = datetime.fromtimestamp(int(x))
相关问题