我有一个像这样的字典列表
"Tournaments": [
{
"Scrambling": "61.90",
"Total_Putts_GIR": 85,
"SG_Putting": 0.99,
"Tournament": "Sony_Open",
"Date": "01-09-2020"
},
{
"Scrambling": "68.75",
"Total_Putts_GIR": 93,
"SG_Putting": 1.1,
"Tournament": "Waste_Management",
"Date": "01-30-2020"
},
{
"Scrambling": "64.71",
"Total_Putts_GIR": 70,
"SG_Putting": -0.26,
"Tournament": "WGC_Mexico",
"Date": "02-20-2020"
},
{
"Scrambling": "57.14",
"Total_Putts_GIR": 40,
"SG_Putting": -1.45,
"Tournament": "Charles_Schwab",
"Date": "06-11-2020"
},
{
"Scrambling": "73.68",
"Total_Putts_GIR": 82,
"SG_Putting": 1.65,
"Tournament": "RBC_Heritage",
"Date": "06-18-2020"
},
{
"Scrambling": "66.67",
"Total_Putts_GIR": 92,
"SG_Putting": 0.34,
"Tournament": "Rocket_Mortgage",
"Date": "07-02-2020"
},
{
"Scrambling": "83.33",
"Total_Putts_GIR": 101,
"SG_Putting": 1.19,
"Tournament": "Shriners_Hospital_for_Children_Open",
"Date": "10-08-2019"
}
]
我用过
for obj in player_stats:
for tournament in obj['Tournaments']:
obj['Tournaments'].sort(key=operator.itemgetter('Date'))
然而,将它们排序,将2019放在底部。当我添加reverse=True
时,它也会逆转月份。我尝试使用obj['Tournaments'].sort(key=lambda x: tournament['Date'])
,但这只会以相同的顺序返回字典。我需要返回按2019、2020年排序,然后按月排序,然后按天排序的字典列表。
答案 0 :(得分:3)
使用格式为YYYYMMDD的排序键。 (您的日期格式为MM-DD-YYYY。)
obj['Tournaments'].sort(key=lambda t:
t["Date"][6:] + t["Date"][:2] + t["Date"][3:5])
print(list(t["Date"] for t in obj["Tournaments"]))
给予:
['10-08-2019', '01-09-2020', '01-30-2020', '02-20-2020', '06-11-2020', '06-18-2020', '07-02-2020']
答案 1 :(得分:1)
日期以字符串表示,并按字典顺序排序。例如,这就是为什么'10-08-2019'
在'07-02-2020'
之后被排序的原因。
一种解决方法是将字符串解析为datetime.datetime
对象以进行排序。
from datetime import datetime
...
key=lambda x: datetime.strptime(x['Date'], '%m-%d-%Y')
obj['Tournaments'].sort(key=key)
答案 2 :(得分:0)
另一个选项 可以是使用pandas
库对词典进行排序。现在,这可能是有争议的,因为有时您可能不需要引入大熊猫,但是这取决于您正在处理的问题/要求的种类。通常,使用熊猫可视化和处理数据通常也更容易。
我将留给您确定是否要使用熊猫来执行此任务,同时我会留意您如何轻松可视化结果并将其与熊猫进行排序。
import pandas as pd
for obj in player_stats:
obj['Tournaments'] = pd.DataFrame(obj['Tournaments']).sort_values(['Date']).T.to_dict(orient='records')
单行解决方案是这样:
pd.DataFrame(obj['Tournaments']).sort_values(['Date']).to_dict(orient='records')
让我们分解一下以了解它的作用。
DataFrame
df = pd.DataFrame(obj['Tournaments']).sort_values(['Date'])
print(df) # print sorted dataframe
DataFrame
转换为dict
# convert DataFrame to dict
d = df.to_dict(orient='records')
# print dictionary using json library
print(json.dumps(d, indent=2)
输出:
[{'Date': '01-09-2020',
'SG_Putting': 0.99,
'Scrambling': '61.90',
'Total_Putts_GIR': 85,
'Tournament': 'Sony_Open'},
{'Date': '01-30-2020',
'SG_Putting': 1.1,
'Scrambling': '68.75',
'Total_Putts_GIR': 93,
'Tournament': 'Waste_Management'},
{'Date': '02-20-2020',
'SG_Putting': -0.26,
'Scrambling': '64.71',
'Total_Putts_GIR': 70,
'Tournament': 'WGC_Mexico'},
{'Date': '06-11-2020',
'SG_Putting': -1.45,
'Scrambling': '57.14',
'Total_Putts_GIR': 40,
'Tournament': 'Charles_Schwab'},
{'Date': '06-18-2020',
'SG_Putting': 1.65,
'Scrambling': '73.68',
'Total_Putts_GIR': 82,
'Tournament': 'RBC_Heritage'},
{'Date': '07-02-2020',
'SG_Putting': 0.34,
'Scrambling': '66.67',
'Total_Putts_GIR': 92,
'Tournament': 'Rocket_Mortgage'},
{'Date': '10-08-2019',
'SG_Putting': 1.19,
'Scrambling': '83.33',
'Total_Putts_GIR': 101,
'Tournament': 'Shriners_Hospital_for_Children_Open'}]
我希望保留问题中提供的样本/虚拟数据以及答案所用的样本/虚拟数据,以便于再现。
obj = {
"Tournaments": [
{
"Scrambling": "61.90",
"Total_Putts_GIR": 85,
"SG_Putting": 0.99,
"Tournament": "Sony_Open",
"Date": "01-09-2020"
},
{
"Scrambling": "68.75",
"Total_Putts_GIR": 93,
"SG_Putting": 1.1,
"Tournament": "Waste_Management",
"Date": "01-30-2020"
},
{
"Scrambling": "64.71",
"Total_Putts_GIR": 70,
"SG_Putting": -0.26,
"Tournament": "WGC_Mexico",
"Date": "02-20-2020"
},
{
"Scrambling": "57.14",
"Total_Putts_GIR": 40,
"SG_Putting": -1.45,
"Tournament": "Charles_Schwab",
"Date": "06-11-2020"
},
{
"Scrambling": "73.68",
"Total_Putts_GIR": 82,
"SG_Putting": 1.65,
"Tournament": "RBC_Heritage",
"Date": "06-18-2020"
},
{
"Scrambling": "66.67",
"Total_Putts_GIR": 92,
"SG_Putting": 0.34,
"Tournament": "Rocket_Mortgage",
"Date": "07-02-2020"
},
{
"Scrambling": "83.33",
"Total_Putts_GIR": 101,
"SG_Putting": 1.19,
"Tournament": "Shriners_Hospital_for_Children_Open",
"Date": "10-08-2019"
}
]
}