Петля внутри цикла – python

Я хочу получить наивысшее количество, проданное для определенного идентификатора продукта, и я застрял. Я не знаю, как добавить еще один цикл, который будет вычислять общее количество каждого идентификатора продукта.

Ниже приведен код и файл 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 

 <?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 srl</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> - <?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 srl</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] 

Explantion: (Чтобы узнать счетчик )

  1. Первый интонационный диктатор. для каждого продукта в качестве ключа и количества в качестве значения
  2. если идентификатор продукта уже присутствует, то добавьте настоящее количество с количеством в productid
  3. счетчик используется для получения максимального значения, т.е. идентификатор продукта с наибольшим количеством