检查dic列表并添加值

时间:2018-11-12 08:49:36

标签: python python-3.x list dictionary

我有以下问题:

我有一个字典列表,想遍历列表( temp_list )并检查:

  • 如果 temp [x] temp [y] 的值 dic [“ z”] 位于 distance_value
  • 如果不是,请在 temp [x] temp [y] 之间插入一个新字典,其中包含 z_value (< > temp [y] -temp [x])/ 2 ),将其命名为 dic_x_y
  • 然后填充新插入的dic( dic_x_y [“ t1”] dic_x_y [“ angle1”] dic_x_y [“ material “] ),而dic的值以 temp [x]

以下是带有列表和变量的数据:

    distance_value = 1000

    temp = [
    {
      "z": 1450,
      "t1": 0,
      "angle1": 90,
      "material": "Balsa 150"
    },
    {
      "z": 1950,
      "t1": 25,
      "angle1": 90,
      "material": "Balsa 150"
    },
    {
      "z": 12800,
      "t1": 25,
      "angle1": 90,
      "material": "Balsa 150"
    },
    {
      "z": 13000,
      "t1": 15,
      "angle1": 90,
      "material": "Balsa 150"
    },
    {
      "z": 25900,
      "t1": 15,
      "angle1": 90,
      "material": "Balsa 150"
    },
    {
      "z": 26000,
      "t1": 10,
      "angle1": 90,
      "material": "Balsa 150"
    }]

我搜索了很多问题,但是找不到答案。 我希望我能清楚地说明我的问题,有人可以帮助我。 提前谢谢。

我真的不知道如何开始,但是那是我的想法,我无法上班:

distance_value = 1000

for dic in temp: 
if "dic["z"] +1 (second element of the list) - dic["z"] < distance_value:
    new_dic = {"z": (dic["z"]+1 - dic["z"]), "t1": dic["t1"] , "angle1":dic["angle1"], "material":dic["material"] }
    temp.insert[dic["z"]+1, new_dic]

1 个答案:

答案 0 :(得分:0)

来自我的 json 测试文件test.json

[
    {
      "z": 1450,
      "t1": 0,
      "angle1": 90,
      "material": "Balsa 150"
    },
    {
      "z": 1950,
      "t1": 25,
      "angle1": 90,
      "material": "Balsa 150"
    },
    {
      "z": 12800,
      "t1": 25,
      "angle1": 90,
      "material": "Balsa 150"
    },
    {
      "z": 13000,
      "t1": 15,
      "angle1": 90,
      "material": "Balsa 150"
    },
    {
      "z": 25900,
      "t1": 15,
      "angle1": 90,
      "material": "Balsa 150"
    },
    {
      "z": 26000,
      "t1": 10,
      "angle1": 90,
      "material": "Balsa 150"
    }]

python 代码:

import json
with open('test.json') as f:
    temp = json.load(f)
distance_value = 1000

temp.sort(key=lambda k: k['z'])
counter = 0
Continue = True
while (Continue):
    for i in range (0,len(temp)-1):
        if(temp[i+1]['z'] - temp[i]['z'] > distance_value):
            Continue = True
            new_dic = {"z": (temp[i+1]['z'] + temp[i]['z'])/2., "t1": temp[i]['t1'], "angle1": 90, "material": temp[i]['material']}
            temp.append(new_dic)
            temp.sort(key=lambda k: k['z'])
            break
        else:
            Continue = False

temp_as_string = json.dumps(temp, sort_keys=True, indent=4, separators=(',', ': '))
print(temp_as_string)

我的输出

[
    [
{
    "z": 1450,
    "t1": 0,
    "angle1": 90,
    "material": "Balsa 150"
},
{
    "z": 1950,
    "t1": 25,
    "angle1": 90,
    "material": "Balsa 150"
},
{
    "z": 2628.125,
    "t1": 25,
    "angle1": 90,
    "material": "Balsa 150"
},
{
    "z": 3306.25,
    "t1": 25,
    "angle1": 90,
    "material": "Balsa 150"
},
{
    "z": 3984.375,
    "t1": 25,
    "angle1": 90,
    "material": "Balsa 150"
},
{
    "z": 4662.5,
    "t1": 25,
    "angle1": 90,
    "material": "Balsa 150"
},
{
    "z": 5340.625,
    "t1": 25,
    "angle1": 90,
    "material": "Balsa 150"
},
{
    "z": 6018.75,
    "t1": 25,
    "angle1": 90,
    "material": "Balsa 150"
},
{
    "z": 6696.875,
    "t1": 25,
    "angle1": 90,
    "material": "Balsa 150"
},
{
    "z": 7375.0,
    "t1": 25,
    "angle1": 90,
    "material": "Balsa 150"
},
{
    "z": 8053.125,
    "t1": 25,
    "angle1": 90,
    "material": "Balsa 150"
},
{
    "z": 8731.25,
    "t1": 25,
    "angle1": 90,
    "material": "Balsa 150"
},
{
    "z": 9409.375,
    "t1": 25,
    "angle1": 90,
    "material": "Balsa 150"
},
{
    "z": 10087.5,
    "t1": 25,
    "angle1": 90,
    "material": "Balsa 150"
},
{
    "z": 10765.625,
    "t1": 25,
    "angle1": 90,
    "material": "Balsa 150"
},
{
    "z": 11443.75,
    "t1": 25,
    "angle1": 90,
    "material": "Balsa 150"
},
{
    "z": 12121.875,
    "t1": 25,
    "angle1": 90,
    "material": "Balsa 150"
},
{
    "z": 12800,
    "t1": 25,
    "angle1": 90,
    "material": "Balsa 150"
},
{
    "z": 13000,
    "t1": 15,
    "angle1": 90,
    "material": "Balsa 150"
},
{
    "z": 13806.25,
    "t1": 15,
    "angle1": 90,
    "material": "Balsa 150"
},
{
    "z": 14612.5,
    "t1": 15,
    "angle1": 90,
    "material": "Balsa 150"
},
{
    "z": 15418.75,
    "t1": 15,
    "angle1": 90,
    "material": "Balsa 150"
},
{
    "z": 16225.0,
    "t1": 15,
    "angle1": 90,
    "material": "Balsa 150"
},
{
    "z": 17031.25,
    "t1": 15,
    "angle1": 90,
    "material": "Balsa 150"
},
{
    "z": 17837.5,
    "t1": 15,
    "angle1": 90,
    "material": "Balsa 150"
},
{
    "z": 18643.75,
    "t1": 15,
    "angle1": 90,
    "material": "Balsa 150"
},
{
    "z": 19450.0,
    "t1": 15,
    "angle1": 90,
    "material": "Balsa 150"
},
{
    "z": 20256.25,
    "t1": 15,
    "angle1": 90,
    "material": "Balsa 150"
},
{
    "z": 21062.5,
    "t1": 15,
    "angle1": 90,
    "material": "Balsa 150"
},
{
    "z": 21868.75,
    "t1": 15,
    "angle1": 90,
    "material": "Balsa 150"
},
{
    "z": 22675.0,
    "t1": 15,
    "angle1": 90,
    "material": "Balsa 150"
},
{
    "z": 23481.25,
    "t1": 15,
    "angle1": 90,
    "material": "Balsa 150"
},
{
    "z": 24287.5,
    "t1": 15,
    "angle1": 90,
    "material": "Balsa 150"
},
{
    "z": 25093.75,
    "t1": 15,
    "angle1": 90,
    "material": "Balsa 150"
},
{
    "z": 25900,
    "t1": 15,
    "angle1": 90,
    "material": "Balsa 150"
},
{
    "z": 26000,
    "t1": 10,
    "angle1": 90,
    "material": "Balsa 150"
}
]
[Finished in 0.096s]

逻辑如下:

  1. 在有条件的情况下运行while循环,我应该继续遍历字典并检查吗? Continue
  2. 在while循环中,循环遍历当前列表项,检查是否有任何可能的list [i + 1] ['z']-list [i] ['z']大于设置的距离(这是检查循环)
  3. 如果找到,则使用中间点Z值制作一个新字典,追加并重新排序(这很重要),然后通过断开{{1},从for循环中断(从第一次出现时从检查循环中断)。 } while循环的条件仍然为真
  4. 在while循环的稍后阶段,当我们遍历所有for循环并检查未找到条件时,则Continue为假,而while循环中断