Python Forum
Printing elements in a tuple with formatting
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Printing elements in a tuple with formatting
#1
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
    ================================================
Reply
#2
Do you know the format method?
>>> xxx = ('Pittsburgh', 493.51)
>>> '{item[0]:<38}{item[1]:>10}'.format(item=xxx)
'Pittsburgh                                493.51'
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020