我对python还很陌生。我只是想知道,如何使输出看起来美观和干净?因为搜索和排序函数的输出如下所示
"[{u'id': 1,
u'name': u'ProPerformance',
u'price': u'$2000',
u'type': u'Treadmill'},
{u'id': 2, u'name': u'Eliptane', u'price': u'$1500', u'type': u'Elliptical'},
{u'id': 5,
u'name': u'SupremeChest',
u'price': u'$4000',
u'type': u'Chest Press'},
{u'id': 12, u'name': u'PowerCage', u'price': u'$5000', u'type': u'Squat'}]
---Sorted by Type---
[{u'id': 5,
u'name': u'SupremeChest',
u'price': u'$4000',
u'type': u'Chest Press'},
{u'id': 2, u'name': u'Eliptane', u'price': u'$1500', u'type': u'Elliptical'},
{u'id': 12, u'name': u'PowerCage', u'price': u'$5000', u'type': u'Squat'},
{u'id': 1,
u'name': u'ProPerformance',
u'price': u'$2000',
u'type': u'Treadmill'}]
我希望我的输出看起来像这样
^{pr2}$有人能帮帮我吗?我已经想了一个小时了,这让我很紧张,任何帮助都将不胜感激。 这是我的密码
def searchEquipment(self,search):
foundList = []
workoutObject =self.loadData(self.datafile)
howmanyEquipment = len(workoutObject["equipment"])
for counter in range(howmanyEquipment):
name = workoutObject["equipment"][counter]["name"].lower()
lowerCaseSearch = search.lower()
didIfindIt = name.find(lowerCaseSearch)
if didIfindIt >= 0:
foundList.append(workoutObject["equipment"][counter])
return foundList
def sortByType(self,foundEquipment):
sortedTypeList = sorted(foundEquipment, key=itemgetter("type"))
return sortedTypeList
我试图用foundList.append(workoutObject["equipment"][counter])
来打印workoutObject["equipment"][counter]["name"]
,但它弄乱了我的排序功能。在
谢谢
简单的回答是研究string formatting operations in Python。如果你看那里,你可以找出如何根据你的需要格式化不同的数据类型。在
一个较长的答案,基本上会让我们为您编写代码,但作为一个初学者:
相关问题 更多 >
编程相关推荐