python - Elementtree and counter - finding corresponding value -


this question exact duplicate of:

i have element tree python code counter. need specific name (product name, supplier name , supplier id) . below code me product id highest quantity sold. need product name, supplier name , supplier id corresponding product id

orders = root.findall("./orders") 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:             print productid,float(quantity[0].text)             print total             total[productid ]=float(quantity[0].text)   print counter(total).most_common(1)[0][0] 


here xml

-<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> 

import xml.etree.elementtree et collections import counter,defaultdict  root = et.elementtree(file="nwind_medium.xml")  orders = root.findall("./orders") total = {} # intalize default dict here productdetails=defaultdict(list,{}) order in orders:     orderdetails = order.findall("./orderdetails")     detail in orderdetails:         productid = detail.findall("./products/productid")[0].text         quantity = detail.findall("./quantity")         supplierid = detail.findall("./suppliers/supplierid")[0].text         companyname = detail.findall("./suppliers/companyname")[0].text         productname = detail.findall("./products/productname")[0].text         #use append add details product details         productdetails[productid].append((supplierid,companyname,productname))         if productid in total.keys():             total[productid]+=float(quantity[0].text)         else:               total[productid]=float(quantity[0].text)  maxprod= counter(total).most_common(1)[0][0] print set(productdetails[maxprod]) 

explanation:

  1. since same product id has different product names,suppliersname,id accomplish need intilize (learn here)defaultdict
  2. then suppling product id product detail details needed

Comments

Popular posts from this blog

javascript - RequestAnimationFrame not working when exiting fullscreen switching space on Safari -

Python ctypes access violation with const pointer arguments -