Loop inside the loop - python -


i want highest quantity sold s specific product id , i'm stuck. don't know how add loop calculate total quantity of each product id.

below code , xml file. thank in advance.

import xml.etree.elementtree et root = et.elementtree(file="nwind_medium.xml")  orders = root.findall("./orders") order in orders:     orderdetails = order.findall("./orderdetails")     total = 0     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 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 = {} order in orders:     orderdetails = order.findall("./orderdetails")     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:(to learn counter)

  1. first intalise dictonary . each productid key , quantity value
  2. if product id present add present quantity quantity in productid
  3. counter used maximum value ie product id quantity

Comments

Popular posts from this blog

google api - Incomplete response from Gmail API threads.list -

Installing Android SQLite Asset Helper -

Qt Creator - Searching files with Locator including folder -