我在文本文件中有以下项目,其中包含城市名称和季度收入:
Atlanta 40 50 23 18
Denver 56 78 34 11
Miami 40 34 18 30
Orlando 55 67 23 11
Memphis 44 23 56 11
Boston 55 67 33 23
Tampa 45 67 54 77
我在列表中对季度收入进行排序时遇到问题。我目前的计划看起来像这样。
with open('revenue.txt', 'r') as f:
text = [line.split() for line in f]
print("{:<12}{:<14}{:>10}{:>9}".format("City", "Revenue", "Total",
"Average"))
for a in text:
city = a[0]
revenue = [int(i) for i in a[1:5]]
total = sum(revenue)
avg = sum(revenue)/len(revenue)
print("{:<12}{}{:>6}{:>9.2f}".format(city, revenue, total, avg))
print("\n""Sort the cities by their Quarter 1 revenues.")
print("\n""Sort the cities by their Quarter 3 revenues.")
print("\n""Sort the cities by their total revenues.")
我试图找出如何对第一季度,第三季度和总列数进行排序,然后打印与其相关的城市。例如:
Sort the cities by their Quarter 1 revenues
['Miami','Atlanta','Memphis','Tampa','Boston','Orlando','Denver']
# [40,40,44,45,55,55,56]
任何帮助将不胜感激。
答案 0 :(得分:1)
我会使用字典和列表来提高排序的可读性以及代码的可用性。我继续并实现了一个打印表格的功能(因为你在你的功能中多次这样做)。我还添加了一个字典列表作为您的新数据集。然后我使用函数sorted
来构建新组织的列表。
with open('revenue.txt', 'r') as f:
def print_table(data):
for value in data:
print("{city:<12}{revenue}{total:>6}{avg:>9.2f}".format(**value))
text = [line.split() for line in f]
data = []
for a in text:
revenue = [int(i) for i in a[1:5]]
data.append({
'city': a[0],
'revenue': revenue,
'total' : sum(revenue),
'avg' : sum(revenue)/len(revenue)
})
print("\n""Original data set.")
print_table(data)
print("\n""Sort the cities by their Quarter 1 revenues.")
print_table(sorted(data,key=lambda a: a['revenue'][0]))
print("\n""Sort the cities by their Quarter 3 revenues.")
print_table(sorted(data,key=lambda a: a['revenue'][2]))
print("\n""Sort the cities by their total revenues.")
print_table(sorted(data,key=lambda a: a['total']))
答案 1 :(得分:1)
您可以使用pandas
。这很简单:
import pandas
with open('revenue.txt', 'r') as f:
text = [line.split() for line in f]
df = pandas.DataFrame(text, columns = ["City", "Revenue", "Total", "Average", "Other"])
df_sorted_by_Total = df.sort_values("Total", ascending=True) # Sort by values of the City column
print(df_sorted_by_Total)
print(df_sorted_by_Total["City"].tolist()) # Print City column as list
输出:
City Revenue Total Average Other
4 Memphis 44 23 56 11
2 Miami 40 34 18 30
0 Atlanta 40 50 23 18
3 Orlando 55 67 23 11
5 Boston 55 67 33 23
6 Tampa 45 67 54 77
1 Denver 56 78 34 11
['Memphis', 'Miami', 'Atlanta', 'Orlando', 'Boston', 'Tampa', 'Denver']
答案 2 :(得分:0)
你可以这样排序:
text = [
["Atlanta", 40,50,23,18 ],
["Denver ", 56,78,34,11 ],
["Miami ", 40,34,18,30 ],
["Orlando", 55,67,23,11 ],
["Memphis", 44,23,56,11 ],
["Boston ", 55,67,33,23 ],
["Tampa ", 45,67,54,77 ]]
for a in text:
city = a[0]
revenue = [int(i) for i in a[1:5]]
total = sum(revenue)
avg = sum(revenue)/len(revenue)
# print("{:<12}{}{:>6}{:>9.2f}{:>9.2f}{:>9.2f}".format(city, revenue, total, avg))
a.append(total) # append it to your former array
a.append(avg) # append it to your former array
print ("1.Q")
printIt(sorted(text, key=lambda x: x[1])) # sort by 1. Quarter number
print ("2.Q")
printIt(sorted(text, key=lambda x: x[2])) # sort by 2. Quarter number
print ("3.Q")
printIt(sorted(text, key=lambda x: x[3])) # etc.
print ("4.Q")
printIt(sorted(text, key=lambda x: x[4]))
print ("Total")
printIt(sorted(text, key=lambda x: x[5]))
print ("Avg")
printIt(sorted(text, key=lambda x: x[6]))
sorted(..)
将创建一个新的列表对象 - 您可以将其简单地存储到您想要使用的内容中 - 出于演示目的,只需简单地打印它们即可。
输出:
1.Q
[ ['Atlanta', 40, 50, 23, 18, 131, 32.75],
['Miami ', 40, 34, 18, 30, 122, 30.5],
['Memphis', 44, 23, 56, 11, 134, 33.5],
['Tampa ', 45, 67, 54, 77, 243, 60.75],
['Orlando', 55, 67, 23, 11, 156, 39.0],
['Boston ', 55, 67, 33, 23, 178, 44.5],
['Denver ', 56, 78, 34, 11, 179, 44.75]]
2.Q
[ ['Memphis', 44, 23, 56, 11, 134, 33.5],
['Miami ', 40, 34, 18, 30, 122, 30.5],
['Atlanta', 40, 50, 23, 18, 131, 32.75],
['Orlando', 55, 67, 23, 11, 156, 39.0],
['Boston ', 55, 67, 33, 23, 178, 44.5],
['Tampa ', 45, 67, 54, 77, 243, 60.75],
['Denver ', 56, 78, 34, 11, 179, 44.75]]
3.Q
[ ['Miami ', 40, 34, 18, 30, 122, 30.5],
['Atlanta', 40, 50, 23, 18, 131, 32.75],
['Orlando', 55, 67, 23, 11, 156, 39.0],
['Boston ', 55, 67, 33, 23, 178, 44.5],
['Denver ', 56, 78, 34, 11, 179, 44.75],
['Tampa ', 45, 67, 54, 77, 243, 60.75],
['Memphis', 44, 23, 56, 11, 134, 33.5]]
4.Q
[ ['Denver ', 56, 78, 34, 11, 179, 44.75],
['Orlando', 55, 67, 23, 11, 156, 39.0],
['Memphis', 44, 23, 56, 11, 134, 33.5],
['Atlanta', 40, 50, 23, 18, 131, 32.75],
['Boston ', 55, 67, 33, 23, 178, 44.5],
['Miami ', 40, 34, 18, 30, 122, 30.5],
['Tampa ', 45, 67, 54, 77, 243, 60.75]]
Total
[ ['Miami ', 40, 34, 18, 30, 122, 30.5],
['Atlanta', 40, 50, 23, 18, 131, 32.75],
['Memphis', 44, 23, 56, 11, 134, 33.5],
['Orlando', 55, 67, 23, 11, 156, 39.0],
['Boston ', 55, 67, 33, 23, 178, 44.5],
['Denver ', 56, 78, 34, 11, 179, 44.75],
['Tampa ', 45, 67, 54, 77, 243, 60.75]]
Avg
[ ['Miami ', 40, 34, 18, 30, 122, 30.5],
['Atlanta', 40, 50, 23, 18, 131, 32.75],
['Memphis', 44, 23, 56, 11, 134, 33.5],
['Orlando', 55, 67, 23, 11, 156, 39.0],
['Boston ', 55, 67, 33, 23, 178, 44.5],
['Denver ', 56, 78, 34, 11, 179, 44.75],
['Tampa ', 45, 67, 54, 77, 243, 60.75]]
这适用于How to sort a list of objects based on an attribute of the objects?中显示的问题。已标记的问题为重复。