dic和附加值检查表

2024-09-27 09:25:15 发布

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

我有以下问题:

我有一个dict列表,想循环查看列表(临时列表),然后检查:

  • 如果temp[x]temp[y]的值dic[“z”]distance\u值的范围内。你知道吗
  • 如果没有,则在temp[x]temp[y]之间插入一个新的dict,其中包含一个z\u值(temp[y]-temp[x])/2),将其命名为dic\u x\u y
  • 然后,用温度[x]中的dic值填充新插入的dic(dic_x_y[“t1”]dic_x_y[“angle1”]dic_x_y[“material”])的剩余值

以下是包含列表和变量的数据:

    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]

Tags: 数据答案列表newvalue温度命名temp
1条回答
网友
1楼 · 发布于 2024-09-27 09:25:15

从我的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循环中,循环当前列表项,检查是否在任何可能的列表[i+1]['z']-列表[i]['z']大于设置的距离(这是检查循环)
  3. 如果找到了,则使用中间点Z值创建一个新的dict,追加并重新排序(这非常重要),然后通过中断while循环的Continue条件来中断for循环(第一次出现时中断检查循环)
  4. 在while循环的稍后阶段,当我们遍历all for循环并检查condition not found时,Continue为false,while循环中断

相关问题 更多 >

    热门问题