Python我需要这个键,在for循环外部访问的值。我怎么做这个球

2024-06-09 08:42:42 发布

您现在位置:Python中文网/ 问答频道 /正文

下面是我的示例代码: 对于键,应用程序\ list.items()中的值: 其中应用程序列表是JSON。 我需要这个键和值,稍后超出for循环的范围,如下所示:

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

我怎样才能做到这一点


Tags: key代码injson应用程序示例列表for
1条回答
网友
1楼 · 发布于 2024-06-09 08:42:42

如果您想存储一个特定的键以备以后使用,只需分配给一个变量,例如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))

相关问题 更多 >