我希望能够为“前3个城市”,“下3个城市”,“前3个项目类别”和“下3个项目类别”打印3个元组(列表中)的元素。但是,我现在唯一能做的就是打印出每个元组,但这不是我真正想要的。
purchases.txt
public void CallButtonFromAnotherRibbon()
{
try
{
SendKeys.Send("%");
SendKeys.Send("Y");
SendKeys.Send("2");
SendKeys.Send("%");
SendKeys.Send("Y");
SendKeys.Send("7");
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString(), "Unexpected Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
test.py
library(readr)
library(dplyr)
library(tidyr)
library(ggplot2)
Ind <- read_csv("Industrial.csv")
Ind1 <- Ind %>%
select(Period,Year,City,Sub_Market,GeoLevel7,Property_Type,Average_Equivalent_Yield_Prime_Grade) %>%
filter(Sub_Market == "Sydney Metro" & GeoLevel7 == "South Sydney" & Property_Type == "Distribution Warehouse/Logistics")%>%
unite(Time,Period,Year,sep=".")
ggplot(data=Ind1, aes(x=Time, y=Average_Equivalent_Yield_Prime_Grade,group=1)) +
geom_line(color="#aa0022", size=1) +
geom_point(color="#aa0022", size=1) +
scale_x_discrete(breaks=c("Q1.1976","Q1.1981","Q1.1986","Q1.1991","Q1.1996","Q1.2001","Q1.2006","Q1.2011","Q1.2016","Q1F.2021","Q1F.2026")) +
ggtitle("South Sydney Cap Rates") +
labs(x="", y="(%)") +
theme(axis.title.y = element_text(size=12, family="Times", color="#666666")) +
theme(axis.text = element_text(size=12, family="Times")) +
theme(plot.title = element_text(size=14, family="Times", face="bold", hjust=0, color="#666666")
我的输出
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
所需的输出
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()
答案 0 :(得分: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
值。
答案 1 :(得分:0)
我解决了!我所要做的就是改变
for i in top3citylist:
print (i)
这样的事情
for i in top3citylist:
print("{0:<29}{1:>18}".format(i[0],i[1]))