无法找到导入数据的平均值 XML
Having trouble finding the average of data from imported XML
必须通过导入 properties.xml 从 XML 数据中找到平均净收入
然后将数据库中包含平均净收入的行发送到键字典。
当我在平均收入函数中使用公式时,什么也没有打印出来。即使我尝试在将它发送到字典之前直接从函数中打印出来。
from helper import fixNumber
import xml.etree.cElementTree as ET
tree = ET.parse("properties.xml")
root = tree.getroot()
list = []
for child in root:
data = child.attrib.copy()
data['netIncome'] = fixNumber(child.text)
data["id"] = (data["id"])
data['cost'] = fixNumber(data['cost'])
data['downPayment'] = fixNumber(data['downPayment'])
data['state'] = (data['state'])
data['percentage'] = fixNumber(data['percentage'])
list.append(data)
def average_income(data):
return data['netIncome']
average = sum(data['netIncome']) / len(data['netIncome'])
print(average)
然后必须将具有平均净收入的数据行发送到此字典
if __name__ == '__main__':
my_func = {
5: average_income
}
for key in my_func:
funct = my_func[key]
print(f"{key}", funct(data))
我收到这个错误
Traceback (most recent call last):
File "/VisualStudio/01.LESSON/properties.py", line 34, in <module>
average = sum(data['netIncome']) / len(data['netIncome'])
TypeError: 'float' object is not iterable
这是代码中调用的 properties.xml 示例
<properties>
<property id="H00001" cost="106000" downPayment="24380" state="NM" percentage="0.12">2925.6</property>
<property id="H00002" cost="125000" downPayment="30000" state="AZ" percentage="0.15">4500</property>
<property id="H00003" cost="119000" downPayment="24990" state="NH" percentage="0.13">3248.7</property>
<property id="H00004" cost="124000" downPayment="31000" state="MI" percentage="0.19">5890</property>
<property id="H00005" cost="143000" downPayment="34320" state="CZ" percentage="0.11">3775.2</property>
<property id="H00006" cost="139000" downPayment="30580" state="VI" percentage="0.12">3669.6</property>
<property id="H00007" cost="132000" downPayment="26400" state="ND" percentage="0.19">5016</property>
<property id="H00008" cost="134000" downPayment="26800" state="CZ" percentage="0.17">4556</property>
<property id="H00009" cost="143000" downPayment="34320" state="PA" percentage="0.14">4804.8</property>
<property id="H00010" cost="123000" downPayment="25830" state="IN" percentage="0.2">5166</property>
</properties>
尝试更改平均值以使用您创建的列表而不是使用 for 循环中的变量:
from helper import fixNumber
import xml.etree.cElementTree as ET
tree = ET.parse("properties.xml")
root = tree.getroot()
data_list = []
for child in root:
data = child.attrib.copy()
data['netIncome'] = fixNumber(child.text)
data["id"] = (data["id"])
data['cost'] = fixNumber(data['cost'])
data['downPayment'] = fixNumber(data['downPayment'])
data['state'] = (data['state'])
data['percentage'] = fixNumber(data['percentage'])
data_list.append(data)
average = sum([i['netIncome'] for i in data_list]) / len(data_list)
print(average)
我还将列表重命名为 data_list 以防止隐藏内置列表关键字。
这还假设您的 fixNumber
函数 returns 是 int
或 float
。
必须通过导入 properties.xml 从 XML 数据中找到平均净收入 然后将数据库中包含平均净收入的行发送到键字典。 当我在平均收入函数中使用公式时,什么也没有打印出来。即使我尝试在将它发送到字典之前直接从函数中打印出来。
from helper import fixNumber
import xml.etree.cElementTree as ET
tree = ET.parse("properties.xml")
root = tree.getroot()
list = []
for child in root:
data = child.attrib.copy()
data['netIncome'] = fixNumber(child.text)
data["id"] = (data["id"])
data['cost'] = fixNumber(data['cost'])
data['downPayment'] = fixNumber(data['downPayment'])
data['state'] = (data['state'])
data['percentage'] = fixNumber(data['percentage'])
list.append(data)
def average_income(data):
return data['netIncome']
average = sum(data['netIncome']) / len(data['netIncome'])
print(average)
然后必须将具有平均净收入的数据行发送到此字典
if __name__ == '__main__':
my_func = {
5: average_income
}
for key in my_func:
funct = my_func[key]
print(f"{key}", funct(data))
我收到这个错误
Traceback (most recent call last):
File "/VisualStudio/01.LESSON/properties.py", line 34, in <module>
average = sum(data['netIncome']) / len(data['netIncome'])
TypeError: 'float' object is not iterable
这是代码中调用的 properties.xml 示例
<properties>
<property id="H00001" cost="106000" downPayment="24380" state="NM" percentage="0.12">2925.6</property>
<property id="H00002" cost="125000" downPayment="30000" state="AZ" percentage="0.15">4500</property>
<property id="H00003" cost="119000" downPayment="24990" state="NH" percentage="0.13">3248.7</property>
<property id="H00004" cost="124000" downPayment="31000" state="MI" percentage="0.19">5890</property>
<property id="H00005" cost="143000" downPayment="34320" state="CZ" percentage="0.11">3775.2</property>
<property id="H00006" cost="139000" downPayment="30580" state="VI" percentage="0.12">3669.6</property>
<property id="H00007" cost="132000" downPayment="26400" state="ND" percentage="0.19">5016</property>
<property id="H00008" cost="134000" downPayment="26800" state="CZ" percentage="0.17">4556</property>
<property id="H00009" cost="143000" downPayment="34320" state="PA" percentage="0.14">4804.8</property>
<property id="H00010" cost="123000" downPayment="25830" state="IN" percentage="0.2">5166</property>
</properties>
尝试更改平均值以使用您创建的列表而不是使用 for 循环中的变量:
from helper import fixNumber
import xml.etree.cElementTree as ET
tree = ET.parse("properties.xml")
root = tree.getroot()
data_list = []
for child in root:
data = child.attrib.copy()
data['netIncome'] = fixNumber(child.text)
data["id"] = (data["id"])
data['cost'] = fixNumber(data['cost'])
data['downPayment'] = fixNumber(data['downPayment'])
data['state'] = (data['state'])
data['percentage'] = fixNumber(data['percentage'])
data_list.append(data)
average = sum([i['netIncome'] for i in data_list]) / len(data_list)
print(average)
我还将列表重命名为 data_list 以防止隐藏内置列表关键字。
这还假设您的 fixNumber
函数 returns 是 int
或 float
。