0

I want to be able to print the elements of 3 tuples (in a list) for "Top 3 Cities", "Bottom 3 Cities", "Top 3 Item Categories" and "Bottom 3 Item Categories". However, the only thing I can do now is to print out each tuple but that is not what I really want.

purchases.txt

2012-01-01  09:00   San Jose    Men's Clothing  214.05  Amex
2012-01-01  09:00   Fort Worth  Women's Clothing    153.57  Visa
2012-01-01  09:00   San Diego   Music   66.08   Cash
2012-01-01  09:00   Pittsburgh  Pet Supplies    493.51  Discover
2012-01-01  09:00   Omaha   Children's Clothing 235.63  MasterCard
2012-01-01  09:00   Stockton    Men's Clothing  247.18  MasterCard
2012-01-01  09:00   Austin  Cameras 379.6   Visa
2012-01-01  09:00   New York    Consumer Electronics    296.8   Cash
2012-01-01  09:00   Corpus Christi  Toys    25.38   Discover
2012-01-01  09:00   Fort Worth  Toys    213.88  Visa

test.py

f = open("purchases.txt")

def separator():
      str = ("="*48)
      print (str)
      return;
   citydict = {}
   catdict = {}
    strmoney=line.split('\t')[4]
      money = float(strmoney)

      if city not in citydict:
         citydict[city] = 0
      citydict[city] += money
      if item not in catdict:
         catdict[item] = 0
      catdict[item] += money   

top3citylist = sorted(citydict.items(),key=lambda x:-x[1])[:3]
   top3catlist = sorted(catdict.items(),key=lambda x:-x[1])[:3]

   bottom3citylist = sorted(citydict.items(),key=lambda x:x[1])[:3]
   bottom3catlist = sorted(catdict.items(),key=lambda x:x[1])[:3]

print("Top Three Cities \n")
      separator()

      for i in top3citylist:

         print (i)

      separator()


      print("Bottom Three Cities \n")
      separator()

      print (bottom3citylist[2])
      print (bottom3citylist[1])
      print (bottom3citylist[0])

      separator()

      print ("\nThe Average Sales from " + str(len(catlist))+ " Item Categories:\n\t\t\t\t\t {:0.2f}".format(avritem))

      print("Top Three Item Categories")
      separator()

      for i in top3catlist:

         print (i)

      separator()


      print("\nBottom Three Item Categories")
      separator()


      print (bottom3catlist[2])
      print (bottom3catlist[1])
      print (bottom3catlist[0])

      separator()      

f.close()

My output

Top Three Cities 

================================================
('Pittsburgh', 493.51)
('Austin', 379.6)
('Fort Worth', 367.45)
================================================
Bottom Three Cities 

================================================
('San Jose', 214.05)
('San Diego', 66.08)
('Corpus Christi', 25.38)
================================================

Top Three Item Categories
================================================
('Pet Supplies', 493.51)
("Men's Clothing", 461.23)
('Cameras', 379.6)
================================================

Bottom Three Item Categories
================================================
("Children's Clothing", 235.63)
("Women's Clothing", 153.57)
('Music', 66.08)
================================================

Desired output

Top Three Cities 

================================================
Pittsburgh                               493.51
Austin                                   379.60
Fort Worth                               367.45
================================================
Bottom Three Cities 

================================================
Omaha                                     235.63
San Jose                                  214.05
San Diego                                  66.08
================================================


Top Three Item Categories
================================================
Pet Supplies                             493.51
Men's Clothing                           461.23
Cameras                                  379.60
================================================

Bottom Three Item Categories
================================================
Children's Clothing                       235.63
Women's Clothing                          153.57
Music                                      66.08
================================================
Cœur
  • 37,241
  • 25
  • 195
  • 267
john tan
  • 123
  • 2
  • 8
  • Possible duplicate of [Obtaining values from columns in python](https://stackoverflow.com/questions/48374603/obtaining-values-from-columns-in-python) – Rakesh Jan 24 '18 at 07:06

2 Answers2

0

Your problem

print (i)

where i is a tuple. The default __str__ method in the tuple will print the contents in between parentheses by calling the str on each item (therefore invoking the __str__ method of each item.

Your separator() call is printing a line of a length which we can define as len_sep. We need that value in order to be able to separate the items in the tuple and meet the end of the string (chain of = signs) produced by separator(). Let's also assume that your tuple always has 2 values. The code to print the two items and separate them ...

for name, value in i:
    str_val = str(value)
    len_space = len_sep - len(name) - len(str_val)
    print('{}{}{}'.format(name, ' ' * len_space, str_val))

Of course you have to provide the len_sep value, which is not provided in your code.

mementum
  • 3,153
  • 13
  • 20
0

I solved it! All I had to do was change

  for i in top3citylist:

     print (i)

to something like this

for i in top3citylist:

     print("{0:<29}{1:>18}".format(i[0],i[1]))
john tan
  • 123
  • 2
  • 8