我想根据他们的销售情况获得前3个城市和项目,但我现在唯一能做的就是返回所有城市和项目及其各自的销售额。不使用dict,我可以获得所需的输出吗?或者,如果我使用dict,我如何获得所需的输出?
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;
city_seen = set()
item_seen = set()
citysaleslist = []
itemsaleslist= []
for line in open(sys.argv[1]):
sales=float(line.split()[-2])
strsales=line.split()[-2]
city=line.split('\t')[2]
item=line.split('\t')[3]
if city not in city_seen: # if city is not a duplicate, add to city_seen set
city_seen.add(city)
#Pressing tab for the bottom 2 lines will remove duplicate but combining the sales for the duplicates is impossible here.
citysales="{0:<29}{1:>18}".format(city,strsales)
citysaleslist.append(citysales)
if item not in item_seen: # if item is not a duplicate, add to item_seen set
item_seen.add(item)
#Pressing tab for the bottom 2 lines will remove duplicate but combining the sales for the duplicates is impossible here.
itemsales = "{0:<29}{1:>18}".format(item,strsales)
itemsaleslist.append(itemsales)
print("Top Three Cities \n")
separator()
for i in citysaleslist:
print(i)
separator()
print("Bottom Three Cities \n")
separator()
separator()
print("Top Three Item Categories")
separator()
for i in itemsaleslist:
print(i)
separator()
print("\nBottom Three Item Categories")
separator()
separator()
我的输出:
Top Three Cities
================================================
San Jose 214.05
Fort Worth 153.57
San Diego 66.08
Pittsburgh 493.51
Omaha 235.63
Stockton 247.18
Austin 379.6
New York 296.8
Corpus Christi 25.38
Fort Worth 213.88
================================================
Bottom Three Cities
================================================
================================================
Top Three Item Categories
================================================
Men's Clothing 214.05
Women's Clothing 153.57
Music 66.08
Pet Supplies 493.51
Children's Clothing 235.63
Men's Clothing 247.18
Cameras 379.6
Consumer Electronics 296.8
Toys 25.38
Toys 213.88
================================================
Bottom Three Item Categories
================================================
================================================
期望的输出:
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.6
================================================
Bottom Three Item Categories
================================================
Toys 239.26
Children's Clothing 235.63
Women's Clothing 153.57
================================================
答案 0 :(得分:0)
您需要对数据进行排序。对数据进行排序后,您可以根据需要打印出数据。
#brief example (but not a direct solution!)
aList = [ ["citya","22"] \
["cityc","44"] \
["cityb","55"] \
]
aSortedList = sorted(alist, key=lamda x:x[1])
#now pick how you want to get your information from the sorted list
#hint you have already read the information.
但您没有直接链接数据。因此,要么创建包含所有信息的新列表,要么随时跟踪信息。
部分问题在于您在阅读文件时尝试立即执行所有操作。读取的数据尚未筛选出来。没有排序,只能阅读。查找最小值和最大值不需要进行排序,但是您需要跟踪信息。
if (new_value > max1):
max3 = max2
max2 = max1
max1 = new_value
if (new_value > max2 and new_value < max1):
...
你将不得不循环使用它。
如果一次性提供所有信息,跟踪就很好。但是,如果信息发生变化,以后引用数据可能会更容易。
答案 1 :(得分:0)
您可以使用字典来获得所需的输出。
两个字典一个城市&amp;另一件物品。然后按字典按字典排序。
示例:强>
import operator
lines = """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""".split("\n")
cities = {}
itemsVal = {}
for i in lines:
val = i.split(" ")
if val[2] not in cities:
cities[val[2]] = float(val[-2])
else:
cities[val[2]] += float(val[-2])
if val[-3] not in itemsVal:
itemsVal[val[-3]] = float(val[-2])
else:
itemsVal[val[-3]] += float(val[-2])
cities = sorted(cities.items(), key=operator.itemgetter(1)) #Sort by sales value
itemsVal = sorted(itemsVal.items(), key=operator.itemgetter(1)) #Sort by sales value
lineSep = "="*48
print("Top Three Cities")
print(lineSep)
for i in reversed(cities[-3:]):
print("{0:<29}{1:>18}".format(i[0], i[1]))
print(lineSep)
print("\n")
print("Bottom Three Cities")
print(lineSep)
for i in cities[:3]:
print("{0:<29}{1:>18}".format(i[0], i[1]))
print(lineSep)
print("\n")
print("Top Three Item Categories")
print(lineSep)
for i in reversed(itemsVal[-3:]):
print("{0:<29}{1:>18}".format(i[0], i[1]))
print(lineSep)
print("\n")
print("Bottom Three Item Categories")
print(lineSep)
for i in itemsVal[:3]:
print("{0:<29}{1:>18}".format(i[0], i[1]))
print(lineSep)
print("\n")
<强>结果:强>
Top Three Cities
================================================
Pittsburgh 493.51
Austin 379.6
Fort Worth 367.45
================================================
Bottom Three Cities
================================================
Corpus Christi 25.38
San Diego 66.08
San Jose 214.05
================================================
Top Three Item Categories
================================================
Pet Supplies 493.51
Men's Clothing 461.23
Cameras 379.6
================================================
Bottom Three Item Categories
================================================
Music 66.08
Women's Clothing 153.57
Children's Clothing 235.63
================================================