循环中的循环-python

循环中的循环-python,python,Python,我想得到最高的销售数量为特定的产品id,我卡住了。我不知道如何添加另一个循环来计算每个产品id的总数量 下面是代码和XML文件。先谢谢你 import xml.etree.ElementTree as ET root = ET.ElementTree(file="nwind_medium.xml") orders = root.findall("./orders") for order in orders: orderdetails = order.findall("./orderde

我想得到最高的销售数量为特定的产品id,我卡住了。我不知道如何添加另一个循环来计算每个产品id的总数量

下面是代码和XML文件。先谢谢你

import xml.etree.ElementTree as ET
root = ET.ElementTree(file="nwind_medium.xml")

orders = root.findall("./orders")
for order in orders:
    orderdetails = order.findall("./orderdetails")
    total = 0
    for detail in orderdetails:
        productid = detail.findall("./products/productid")
        quantity = detail.findall("./quantity")
        total += float(quantity[0].text)

print total
解释:学习

首先是完整的口述。对于每个productid作为键,数量作为值 如果产品id已存在,则将当前数量与产品id中的数量相加 计数器用于获取最大值,即数量最多的产品id
我在total[productid]=floatquantity[0]下收到错误未损坏类型:“list”。text我确实尝试更改XML文件上的数量,但出于某种原因,即使我在不同的产品id上更改了更高的数量,它仍然给我51。我更改了顶部的数量。@user_new_in_python能给我提供合适的示例文件吗?我不知道如何在这里附加文件new in stack overflow,但XML文件是我最初问题底部的文件。我把整件事都公布了。谢谢。natarajСаааа是粘贴的XML适合您还是您需要我向您发送文件?从集合导入XML.etree.ElementTreefile作为ET root=ET.ElementTreefile=nwind_medium.XML导入Counter orders=root.findall./orders中的订单:orderdetails=order.findall./orderdetails total={}对于orderdetails中的详细信息:productid=detail.findall./productid quantity=detail.findall./quantity如果productid为total.key:total[product]+=floatquantity[0]。text-else:total[productid]=floatquantity[0]。text-print-countertall.most\u-common1Hi,这次代码继续运行,没有任何错误。它给了我['51',35.0]作为输出。如果我只想要51作为输出打印输出呢。我想要这个,因为这是数量最多的。
<?xml version="1.0"?>

-<nwind>


-<orders another="Friday" orderid="10248">


-<customers>

<companyname>Vins et alcools Chevalier</companyname>

<customerid>VINET</customerid>

</customers>


-<orderdetails>


-<products>

<productid>72</productid>

<productname>Mozzarella di Giovanni</productname>

</products>

<unitprice>34.8</unitprice>

<quantity>5</quantity>


-<suppliers>

<supplierid>14</supplierid>

<companyname>Formaggi Fortini s.r.l.</companyname>

</suppliers>

</orderdetails>


-<orderdetails>


-<products>

<productid>11</productid>

<productname>Queso Cabrales</productname>

</products>

<unitprice>14</unitprice>

<quantity>12</quantity>


-<suppliers>

<supplierid>5</supplierid>

<companyname>Cooperativa de Quesos 'Las Cabras'</companyname>

</suppliers>

</orderdetails>


-<orderdetails>


-<products>

<productid>42</productid>

<productname>Singaporean Hokkien Fried Mee</productname>

</products>

<unitprice>9.8</unitprice>

<quantity>10</quantity>


-<suppliers>

<supplierid>20</supplierid>

<companyname>Leka Trading</companyname>

</suppliers>

</orderdetails>

</orders>


-<orders orderid="10249">


-<customers>

<companyname>Toms Spezialitaten</companyname>

<customerid>TOMSP</customerid>

</customers>


-<orderdetails>


-<products>

<productid>14</productid>

<productname>Tofus</productname>

</products>

<unitprice>18.6</unitprice>

<quantity>9</quantity>


-<suppliers>

<supplierid>6</supplierid>

<companyname>Mayumi's</companyname>

</suppliers>

</orderdetails>


-<orderdetails>


-<products>

<productid>51</productid>

<productname>Manjimup Dried Apples</productname>

</products>

<unitprice>42.4</unitprice>

<quantity>40</quantity>


-<suppliers>

<supplierid>24</supplierid>

<companyname>G'day, Mate</companyname>

</suppliers>

</orderdetails>

</orders>


-<orders orderid="10250">


-<customers>

<companyname>Hanari Carnes</companyname>

<customerid>HANAR</customerid>

</customers>


-<orderdetails>


-<products>

<productid>65</productid>

<productname>Louisiana Fiery Hot Pepper Sauce</productname>

</products>

<unitprice>16.8</unitprice>

<quantity>15</quantity>


-<suppliers>

<supplierid>2</supplierid>

<companyname>New Orleans Cajun Delights</companyname>

</suppliers>

</orderdetails>


-<orderdetails>


-<products>

<productid>41</productid>

<productname>Jack's New England Clam Chowder</productname>

</products>

<unitprice>7.7</unitprice>

<quantity>10</quantity>


-<suppliers>

<supplierid>19</supplierid>

<companyname>New England Seafood Cannery</companyname>

</suppliers>

</orderdetails>


-<orderdetails>


-<products>

<productid>51</productid>

<productname>Manjimup Dried Apples</productname>

</products>

<unitprice>42.4</unitprice>

<quantity>35</quantity>


-<suppliers>

<supplierid>24</supplierid>

<companyname>G'day, Mate</companyname>

</suppliers>

</orderdetails>

</orders>

</nwind>
from collections import Counter
total = {}
for order in orders:
    orderdetails = order.findall("./orderdetails")
    for detail in orderdetails:
        productid = detail.findall("./products/productid")[0].text
        quantity = detail.findall("./quantity")

        if productid in total.keys():
            total[productid]+=float(quantity[0].text)
        else:

            total[productid]=float(quantity[0].text)         

print "Higest Total Product id:",Counter(total).most_common(1)[0][0]