我正在嘗試從 XML 中對匯入的資料進行排序,然后將其切片以獲取前 10 個并將其回傳到 my_func 字典,然后從 for 回圈呼叫的鍵中列印出資料行
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 top_ten(data): # sort top ten by cost in descending order
return data['cost']
list.sort(key=top_ten, reverse=True)
for x in list[:10]:
print(x)
if __name__ == '__main__':
my_func = {
1: top_ten
}
for key in my_func:
funct = my_func[key]
print(f"{key}", funct())
這是我不斷收到的 TypeError
Traceback (most recent call last):
File "/Users/school/VisualStudio/01.LESSON/properties.py", line 40, in <module>
print(f"{key}", funct())
TypeError: top_ten() missing 1 required positional argument: 'data'
當我在函式的括號內鍵入資料時,它只列印一個屬性的一個資料點,即。1 144000.0 這只是資料中的一項凈收入
我需要列印出所有屬性的前 10 行以及所有資料,并進行排序。希望這是有道理的。
這是 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>
輸出應如下所示,但按成本排序
1 {'id': 'H00012', 'cost': 150000.0, 'downPayment': 36000.0, 'state': 'OR', 'percentage': 0.11, 'netIncome': 3960.0}
{'id': 'H00061', 'cost': 150000.0, 'downPayment': 34500.0, 'state': 'MD', 'percentage': 0.1, 'netIncome': 3450.0}
{'id': 'H00072', 'cost': 150000.0, 'downPayment': 31500.0, 'state': 'MI', 'percentage': 0.15, 'netIncome': 4725.0}
2 {'id': 'H00012', 'cost': 150000.0, 'downPayment': 36000.0, 'state': 'OR', 'percentage': 0.11, 'netIncome': 3960.0}
{'id': 'H00061', 'cost': 150000.0, 'downPayment': 34500.0, 'state': 'MD', 'percentage': 0.1, 'netIncome': 3450.0}
{'id': 'H00072', 'cost': 150000.0, 'downPayment': 31500.0, 'state': 'MI', 'percentage': 0.15, 'netIncome': 4725.0}
uj5u.com熱心網友回復:
您的代碼中有一些需要改進的地方,但是直接跳到解決方案,您需要調整這些內容:
- 您的排序功能
def top_ten(ys): # sort top ten by cost in descending order
# sort the list of dicts by desc order
sorted_list = sorted(ys, key=lambda d: -d['cost'])
# print first 10 elements
for elem in sorted_list[:10]:
print(elem)
- 你的主要電話應該是
if __name__ == '__main__':
my_func = {
1: top_ten
}
for key in my_func:
funct = my_func[key]
print(f"{key}", funct(list))
通過這些更改,您應該得到這個輸出
1:
{'id': 'H00005', 'cost': 143000.0, 'downPayment': 34320.0, 'state': 'CZ', 'percentage': 0.11, 'netIncome': 3775.2}
{'id': 'H00009', 'cost': 143000.0, 'downPayment': 34320.0, 'state': 'PA', 'percentage': 0.14, 'netIncome': 4804.8}
{'id': 'H00006', 'cost': 139000.0, 'downPayment': 30580.0, 'state': 'VI', 'percentage': 0.12, 'netIncome': 3669.6}
{'id': 'H00008', 'cost': 134000.0, 'downPayment': 26800.0, 'state': 'CZ', 'percentage': 0.17, 'netIncome': 4556.0}
{'id': 'H00007', 'cost': 132000.0, 'downPayment': 26400.0, 'state': 'ND', 'percentage': 0.19, 'netIncome': 5016.0}
{'id': 'H00002', 'cost': 125000.0, 'downPayment': 30000.0, 'state': 'AZ', 'percentage': 0.15, 'netIncome': 4500.0}
{'id': 'H00004', 'cost': 124000.0, 'downPayment': 31000.0, 'state': 'MI', 'percentage': 0.19, 'netIncome': 5890.0}
{'id': 'H00010', 'cost': 123000.0, 'downPayment': 25830.0, 'state': 'IN', 'percentage': 0.2, 'netIncome': 5166.0}
{'id': 'H00003', 'cost': 119000.0, 'downPayment': 24990.0, 'state': 'NH', 'percentage': 0.13, 'netIncome': 3248.7}
{'id': 'H00001', 'cost': 106000.0, 'downPayment': 24380.0, 'state': 'NM', 'percentage': 0.12, 'netIncome': 2925.6}
更簡潔的代碼版本:
import xml.etree.cElementTree as ET
def load_data(fpath="properties.xml"):
tree = ET.parse(fpath)
root = tree.getroot()
xs = []
for child in root:
data = child.attrib.copy()
data['netIncome'] = float(child.text)
data["id"] = data["id"]
data['cost'] = float(data['cost'])
data['downPayment'] = float(data['downPayment'])
data['state'] = data['state']
data['percentage'] = float(data['percentage'])
xs.append(data)
return xs
def top_ten(ys): # sort top ten by cost in descending order
sorted_list = sorted(ys, key=lambda d: -d['cost'])
for elem in sorted_list[:10]:
print(elem)
def get_avg_netincome(xs):
c = 0
for x in xs:
c = x['netIncome']
print(f'Average net income: {round(c / len(xs), 2)}')
if __name__ == '__main__':
my_func = {
1: top_ten,
2: get_avg_netincome,
}
for funct_id, funct in my_func.items():
print(f'Function id: {funct_id}')
data = load_data()
funct(data)
print()
產生相同的輸出:
Function id: 1
{'id': 'H00005', 'cost': 143000.0, 'downPayment': 34320.0, 'state': 'CZ', 'percentage': 0.11, 'netIncome': 3775.2}
{'id': 'H00009', 'cost': 143000.0, 'downPayment': 34320.0, 'state': 'PA', 'percentage': 0.14, 'netIncome': 4804.8}
{'id': 'H00006', 'cost': 139000.0, 'downPayment': 30580.0, 'state': 'VI', 'percentage': 0.12, 'netIncome': 3669.6}
{'id': 'H00008', 'cost': 134000.0, 'downPayment': 26800.0, 'state': 'CZ', 'percentage': 0.17, 'netIncome': 4556.0}
{'id': 'H00007', 'cost': 132000.0, 'downPayment': 26400.0, 'state': 'ND', 'percentage': 0.19, 'netIncome': 5016.0}
{'id': 'H00002', 'cost': 125000.0, 'downPayment': 30000.0, 'state': 'AZ', 'percentage': 0.15, 'netIncome': 4500.0}
{'id': 'H00004', 'cost': 124000.0, 'downPayment': 31000.0, 'state': 'MI', 'percentage': 0.19, 'netIncome': 5890.0}
{'id': 'H00010', 'cost': 123000.0, 'downPayment': 25830.0, 'state': 'IN', 'percentage': 0.2, 'netIncome': 5166.0}
{'id': 'H00003', 'cost': 119000.0, 'downPayment': 24990.0, 'state': 'NH', 'percentage': 0.13, 'netIncome': 3248.7}
{'id': 'H00001', 'cost': 106000.0, 'downPayment': 24380.0, 'state': 'NM', 'percentage': 0.12, 'netIncome': 2925.6}
Function id: 2
Average net income: 4355.19
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/427397.html
