2016-11-08 60 views
-1

我有一個帶有書籍信息的JSON文件。 在原始文件中還有更多。來自帶有str.split的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"}] 

現在我想排序基於作者的姓氏的信息。 我做了以下內容:

names = [] 
for a in jsondata: 
names.append(str.split((a['author']))) 

print (sorted(names)) 

然後我得到這樣的信息:

[['Allan', 'Downey'], ['Allan', 'Downey'] 

我在想,如果有人可以給我一個提示/提示要使用什麼我怎麼可以排序依據的所有信息在作者的姓氏上。

回答

0

試試這個:

names = [] 
for a in jsondata: 
    last_name = a["author"].split(" ")[1] 
    names.append(last_name) 

print(sorted(names)) 

如果你想獲得排序的詞典,你可以做這樣的:

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"])) 
+0

做或不​​做。沒有「嘗試」。一個好的答案***將總是解釋所做的事情以及爲什麼這樣做,不僅是爲了OP,還是爲了將來訪問SO。 –

1

您可以使用sorted()lambda表達排序的JSON結構:

lambda x: x["author"].split()[-1] 
# split value of "author", key and sort based on the last word 

有沒有必要明確iter在列表上吃東西,並創建另一個列表來維護名稱。

採樣運行:

>>> 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'}] 
0

你只需要到jsondata清單,從每個字典的「作者」鍵關聯的值提取姓氏適當的鍵功能排序。

我已經使用了.rsplit方法,所以我們可以有效地處理超過2個名字的suthors。 .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" 
    } 
]