Python 使用str.split对JSON文件中的值进行排序
我有一个包含书籍信息的JSON文件。 在原始文件中还有更多 例如:Python 使用str.split对JSON文件中的值进行排序,python,json,Python,Json,我有一个包含书籍信息的JSON文件。 在原始文件中还有更多 例如: [{"author": "Wes McKinney", "price": 53, "title": "Python for Data Analysis", "publication_year": "2012", "topic": "programming"}, {"author": "Joel Grus", "price": 66, "title": "Data Science from Scra
[{"author": "Wes McKinney", "price": 53, "title": "Python for Data Analysis", "publication_year": "2012", "topic": "programming"},
{"author": "Joel Grus", "price": 66, "title": "Data Science from Scratch", "publication_year": "2015", "topic": "Python"}]
现在我想根据作者的姓氏对信息进行排序。
我做了以下工作:
names = []
for a in jsondata:
names.append(str.split((a['author'])))
print (sorted(names))
然后我得到如下信息:
[['Allan', 'Downey'], ['Allan', 'Downey']
for a in sorted(jsondata, key=lambda x: x["author"].split(" ")[1]):
# You can use dict any way you like. E.g.:
print(a["title"] + " " + str(a["price"]))
我想知道是否有人能给我一个提示/提示,如何根据作者的姓氏对所有信息进行排序。试试这个:
names = []
for a in jsondata:
last_name = a["author"].split(" ")[1]
names.append(last_name)
print(sorted(names))
如果您想获得已排序的词典,您可以这样做:
[['Allan', 'Downey'], ['Allan', 'Downey']
for a in sorted(jsondata, key=lambda x: x["author"].split(" ")[1]):
# You can use dict any way you like. E.g.:
print(a["title"] + " " + str(a["price"]))
您可以使用
sorted()
和lambda
表达式对JSON结构进行排序,如下所示:
lambda x: x["author"].split()[-1]
# split value of "author", key and sort based on the last word
不需要显式地迭代列表,并创建另一个列表来维护名称
样本运行:
>>> my_json = [
{"author": "Wes McKinney", "price": 53, "title": "Python for Data Analysis", "publication_year": "2012", "topic": "programming"},
{"author": "Joel Grus", "price": 66, "title": "Data Science from Scratch", "publication_year": "2015", "topic": "Python"}
]
>>> sorted(my_json, key=lambda x: x["author"].split()[-1])
[{'topic': 'Python', 'price': 66, 'title': 'Data Science from Scratch', 'publication_year': '2015', 'author': 'Joel Grus'}, {'topic': 'programming', 'price': 53, 'title': 'Python for Data Analysis', 'publication_year': '2012', 'author': 'Wes McKinney'}]
您只需使用适当的键函数对
jsondata
列表进行排序,该函数从与每个dict的“author”键关联的值中提取姓氏
我使用了.rsplit
方法,因此我们可以有效地处理拥有两个以上姓名的Suthor.rsplit(None,1)
从右侧拆分空白字符串,最多拆分一次,返回一个包含(最多)两个元素的列表。姓氏将是该列表的最后一个元素。如果您希望“Guido Van Rossum”的姓为“Van Rossum”,则需要使用不同的拆分策略
import json
jsondata = [
{
"author": "Wes McKinney", "price": 53,
"title": "Python for Data Analysis",
"publication_year": "2012", "topic": "programming"
},
{
"author": "Joel Grus", "price": 66,
"title": "Data Science from Scratch",
"publication_year": "2015", "topic": "Python"
},
{
"author": "One",
},
{
"author": "Person With A Long Name",
},
]
def last_name(d):
return d["author"].rsplit(None, 1)[-1]
# Verify that `last_name` does what we want
for d in jsondata:
print(last_name(d))
jsondata.sort(key=last_name)
print(json.dumps(jsondata, indent=4))
输出
McKinney
Grus
One
Name
[
{
"title": "Data Science from Scratch",
"author": "Joel Grus",
"topic": "Python",
"publication_year": "2015",
"price": 66
},
{
"title": "Python for Data Analysis",
"author": "Wes McKinney",
"topic": "programming",
"publication_year": "2012",
"price": 53
},
{
"author": "Person With A Long Name"
},
{
"author": "One"
}
]
做或不做。没有“尝试”。一个好的答案总是会有一个解释,说明做了什么以及为什么这样做,不仅是为了OP,而且是为了未来的访客。