如何使用动态生成的密钥访问嵌套在另一个字典中的字典?

时间:2017-10-19 15:42:04

标签: python dictionary flask

所以我有一个看起来像这样的函数:

{'a1': {'description': 'b1', 'price': 'c1', 'qty': 'd1'}, 'a2': {'description': 'b2', 'price': 'c2', 'qty': 'd2'}, 'total': 1.0,}.

字典看起来像:<table> <tr> <td>b1</td><td>c1</td><td>d1</td> <td>.....................................</td> <td>.....................................</td> </tr> <tr> <td>1.0</td> </tr> </table>

然后我需要能够遍历flask中的字典来生成一个如下所示的表:

$array1 = ["fname" => "NULL", "lname" => "NULL", "id" => "NULL"];

$array2 = ["john", "smith", "11123"];

$new_array = array_combine(array_keys($array1), $array2);

print_r($new_array);

我无法弄清楚如何首先只知道其值是另一个字典的键而不知道它们是键,然后如何在不知道密钥的情况下实际访问嵌套字典中的项目。

可能有一个非常明显的答案,但我无法思考如何正确地提出问题以便在其他地方查找

2 个答案:

答案 0 :(得分:0)

这样的事情:

<div>{{ yourObject | json}}</div>

答案 1 :(得分:0)

您似乎需要一个表来存储和访问数据

glDeleteVertexArrays()

访问数据

  while new_string[:4]:
      # Get group of 4
      new_string_line = new_string[:4]

      # Split data
      location = new_string_line[0]
      description = new_string_line[1]
      price = new_string_line[2]
      qty = new_string_line[3]

      results.append([location, description, price, qty])

      # Remove used data
      new_string = new_string[4:]

  df = pd.DataFrame(results, columns=['location', 'description', 'price', 'qty'])

  # if you want  total number 
  total = (df.price * df.qty).sum()


  location description price qty
0       a1          b1    c1  d1
1       a2          b2    c2  d2
相关问题