gpt4 book ai didi

python - 在 for 循环中添加 if 语句

转载 作者:行者123 更新时间:2023-11-28 18:56:37 26 4
gpt4 key购买 nike

来自这个数据框:

tree       cues                        directions   thresholds   exits
1 PLC2hrOGTT;Age;BMI;TimesPregnant >;>;>;> 126;29;29.7;6 1;0;1;0.5
2 PLC2hrOGTT;Age;BMI >;>;> 126;29;29.7 0;1;0.5
3 PLC2hrOGTT;Age;BMI;TimesPregnant >;>;>;> 126;29;29.7;6 1;0;0;0.5
4 PLC2hrOGTT;Age;BMI;TimesPregnant >;>;>;> 126;29;29.7;6 1;1;0;0.5
5 PLC2hrOGTT;Age;BMI;TimesPregnant >;>;>;> 126;29;29.7;6 0;1;0;0.5
6 PLC2hrOGTT;Age;BMI >;>;> 126;29;29.7 0;0;0.5
7 PLC2hrOGTT;Age;BMI;TimesPregnant >;>;>;> 126;29;29.7;6 1;1;1;0.5
8 PLC2hrOGTT;Age;BMI;TimesPregnant >;>;>;> 126;29;29.7;6 0;0;0;0.5

使用此代码:

>>> def row_to_tree(row):
... out = {}
... pos = [out]
... for cues, directions, thresholds, exits in zip(*map(lambda x: x.split(";"), row[["cues", "directions", "thresholds", "exits"]].values)):
... pos = pos[0]
... pos["cues"] = cues
... pos["directions"] = directions
... pos["thresholds"] = thresholds
... pos["exits"] = exits
... pos["children"] = [{"cues":True}]
... pos = pos["children"]
... pos.append({"cues": False})
... return out

我可以得到这个想要的输出:

>>> trees = [row_to_tree(row) for i, row in df.iterrows()]
>>> print(json.dumps(trees[0], indent=2))
{
"cues": "PLC2hrOGTT",
"directions": ">",
"thresholds": "126",
"exits": "1",
"children": [
{
"cues": "Age",
"directions": ">",
"thresholds": "29",
"exits": "0",
"children": [
{
"cues": "BMI",
"directions": ">",
"thresholds": "29.7",
"exits": "1",
"children": [
{
"cues": "TimesPregnant",
"directions": ">",
"thresholds": "6",
"exits": "0.5",
"children": [
{
"cues": true
},
{
"cues": false
}
]
}
]
}
]
}
]
}

但我现在想在这个循环中添加一个 if 语句,所以我现在想要的是当我添加子项时我想要一个 if 以便如果 exits==1 首先添加 True 然后添加子项,如果不是然后先添加 child 再添加False。 (即父级 'PLC2hrOGTT' 应该有 "cues": True 然后是 "cues": "Age"因为 "exits": 在 'PLC2hrOGTT' 中是 "1")我可以在这个循环中实现这个吗?

这是期望的输出:

{
"cues": "PLC2hrOGTT",
"directions": ">",
"thresholds": "126",
"exits": "1",
"children": [
{
"cues": "True",
},
{
"cues": "Age",
"directions": ">",
"thresholds": "29",
"exits": "0",
"children": [
{
"cues": "BMI",
"directions": ">",
"thresholds": "29.7",
"exits": "1",
"children": [
{
"cues": "True",
},
{
"cues": "TimesPregnant",
"directions": ">",
"thresholds": "6",
"exits": "0.5",
"children":[
{
"cues": "True"
},
{
"cues": "False"
}
]
}
]
},
{
"cues": "False"
}
]
}
]
}

最佳答案

要实现你想要的,需要创建两个字典来收集数据并根据条件重新定位输入元素。顺便说一句,你对嵌套字典使用了很好的技巧。代码中的描述。

=^..^=

import pandas as pd
from io import StringIO
import json


data = StringIO("""
tree cues directions thresholds exits
1 PLC2hrOGTT;Age;BMI;TimesPregnant >;>;>;> 126;29;29.7;6 1;0;1;0.5
2 PLC2hrOGTT;Age;BMI >;>;> 126;29;29.7 0;1;0.5
3 PLC2hrOGTT;Age;BMI;TimesPregnant >;>;>;> 126;29;29.7;6 1;0;0;0.5
4 PLC2hrOGTT;Age;BMI;TimesPregnant >;>;>;> 126;29;29.7;6 1;1;0;0.5
5 PLC2hrOGTT;Age;BMI;TimesPregnant >;>;>;> 126;29;29.7;6 0;1;0;0.5
6 PLC2hrOGTT;Age;BMI >;>;> 126;29;29.7 0;0;0.5
7 PLC2hrOGTT;Age;BMI;TimesPregnant >;>;>;> 126;29;29.7;6 1;1;1;0.5
8 PLC2hrOGTT;Age;BMI;TimesPregnant >;>;>;> 126;29;29.7;6 0;0;0;0.5
""")

# load data into data frame
df = pd.read_csv(data, sep=' ')


def row_to_tree(row):
out = {}
data = [{}, out]
position = 1
for cues, directions, thresholds, exits in zip(*map(lambda x: x.split(";"), row[["cues", "directions", "thresholds", "exits"]].values)):
data = data[position] # initialise dictionary
# add elements to dictionary
data['cues'] = cues
data['directions'] = directions
data['tresholds'] = thresholds
data['exits'] = exits

# add new empty dictionary at the end
# handle dictionary starting position selection
if int(float(exits)) == 1:
position = 1
data["children"] = [{"cues":True},{}]
else:
position = 0
data["children"] = [{"cues":True},{"cues":False}]

# relocate new dictionary to the end
data = data["children"]

return out

trees = [row_to_tree(row) for i, row in df.iterrows()]
print(json.dumps(trees[0], indent=2))

输出:

{
"cues": "PLC2hrOGTT",
"directions": ">",
"tresholds": "126",
"exits": "1",
"children": [
{
"cues": true
},
{
"cues": "Age",
"directions": ">",
"tresholds": "29",
"exits": "0",
"children": [
{
"cues": "BMI",
"directions": ">",
"tresholds": "29.7",
"exits": "1",
"children": [
{
"cues": true
},
{
"cues": "TimesPregnant",
"directions": ">",
"tresholds": "6",
"exits": "0.5",
"children": [
{
"cues": true
},
{
"cues": false
}
]
}
]
},
{
"cues": false
}
]
}
]
}

关于python - 在 for 循环中添加 if 语句,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57493465/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com