在元组中打印元素,不带逗号,引号和括号

问题描述 投票:0回答:2

我希望能够为“前3个城市”,“下3个城市”,“前3个项目类别”和“下3个项目类别”打印3个元组(列表中)的元素。但是,我现在唯一能做的就是打印出每个元组,但这不是我真正想要的。

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.朋友

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

我的输出

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

期望的输出

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
================================================
python tuples
2个回答
0
投票

你的问题

print (i)

其中i是一个元组。元组中的默认__str__方法将通过调用每个项目上的str在括号之间打印内容(因此调用每个项目的__str__方法)。

你的separator()调用是打印一条长度的线,我们可以定义为len_sep。我们需要这个值,以便能够分离元组中的项目并满足=生成的字符串(separator()标志链)的末尾。我们还假设您的元组总是有2个值。打印这两个项目并将它们分开的代码......

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

当然,您必须提供len_sep值,这在您的代码中未提供。


0
投票

我解决了!我所要做的就是改变

  for i in top3citylist:

     print (i)

这样的事情

for i in top3citylist:

     print("{0:<29}{1:>18}".format(i[0],i[1]))
© www.soinside.com 2019 - 2024. All rights reserved.