2016-11-29 4 views
0

同じ場所idを共有するデータの複数のインスタンスは、例えば以下の出力で3の多くがあります:私はすべてのスタイルでこれらをソートしたいpythonファイルの出力を場所別にソートします。共通IDによる増分

121 {'data': {'id': 3, 'type': 'location'}, 'links': {'self': 'http://localhost:2510/api/v2/jobs/121/location'}} 
    122 {'data': {'id': 3, 'type': 'location'}, 'links': {'self': 'http://localhost:2510/api/v2/jobs/122/location'}} 
    120 {'data': {'id': 3, 'type': 'location'}, 'links': {'self': 'http://localhost:2510/api/v2/jobs/120/location'}} 
    119 {'data': {'id': 3, 'type': 'location'}, 'links': {'self': 'http://localhost:2510/api/v2/jobs/119/location'}} 
    191 {'data': {'id': 3, 'type': 'location'}, 'links': {'self': 'http://localhost:2510/api/v2/jobs/191/location'}} 
    190 {'data': {'id': 52, 'type': 'location'}, 'links': {'self': 'http://localhost:2510/api/v2/jobs/190/location'}} 
    193 {'data': {'id': 3, 'type': 'location'}, 'links': {'self': 'http://localhost:2510/api/v2/jobs/193/location'}} 
    187 {'data': {'id': 3, 'type': 'location'}, 'links': {'self': 'http://localhost:2510/api/v2/jobs/187/location'}} 
    189 {'data': {'id': 52, 'type': 'location'}, 'links': {'self': 'http://localhost:2510/api/v2/jobs/189/location'}} 
    186 {'data': {'id': 3, 'type': 'location'}, 'links': {'self': 'http://localhost:2510/api/v2/jobs/186/location'}} 
    198 {'data': {'id': 3, 'type': 'location'}, 'links': {'self': 'http://localhost:2510/api/v2/jobs/198/location'}} 
    196 {'data': {'id': 3, 'type': 'location'}, 'links': {'self': 'http://localhost:2510/api/v2/jobs/196/location'}} 
    199 {'data': {'id': 3, 'type': 'location'}, 'links': {'self': 'http://localhost:2510/api/v2/jobs/199/location'}} 
    201 {'data': {'id': 3, 'type': 'location'}, 'links': {'self': 'http://localhost:2510/api/v2/jobs/201/location'}} 

は:

{'data': {'id': 3, 'type': 'location'} 15 
{'data': {'id': 4, 'type': 'location'} 6 
{'data': {'id': 5, 'type': 'location'} 0 
{'data': {'id': 6, 'type': 'location'} 11 

そのようなデータを出力するためにPythonスクリプトを適用する方法はありますか?

は、実際にはそれほどのように見えるこのJSONファイルから来ています:

{ 
     "links": { 
      "self": "http://localhost:2510/api/v2/jobs?skills=data%20science" 
     }, 
     "data": [ 
      { 
       "id": 121, 
       "type": "job", 
       "attributes": { 
        "title": "Data Scientist", 
        "date": "2014-01-22T15:25:00.000Z", 
        "description": "Data scientists are in increasingly high demand amongst tech companies in London. Generally a combination of business acumen and technical skills are sought. Big data experience ..." 
       }, 
       "relationships": { 
        "location": { 
         "links": { 
          "self": "http://localhost:2510/api/v2/jobs/121/location" 
         }, 
         "data": { 
          "type": "location", 
          "id": 3 
         } 
        }, 
        "country": { 
         "links": { 
          "self": "http://localhost:2510/api/v2/jobs/121/country" 
         }, 
         "data": { 
          "type": "country", 
          "id": 1 
         } 
        }, 
        "skills": { 
         "links": { 

と次のPythonスクリプト使用して解析された:

import json 
from pprint import pprint 

with open('data.json') as data_file: 
    data = json.load(data_file) 


    for item in data["data"]: 
     print(item['id'], item['relationships']['location']) 

This is the full data file in my GitHubを。

+1

Python 'dict'sは発注されていません。 orderingを強制できる 'dict'のようなコンテナを使いたい場合は' collections'モジュールから 'OrderedDict'をチェックしてください。 –

+0

はい、しかし、 "data" dictsはリストにあり、ソートすることはできます*。いずれにしても、私が誤解していない限り、OPはまたカウントしたいと思うようです。 –

+0

ああ、最後の列はカウントです。私は 'dict'が必要でないと命令したと思います。 –

答えて

1

私が正しく理解していれば、あなたはこのような構造で項目のリストを持っている:

... 

{{'data': {'id': 3, 'type': 'location'} ... } 
{{'data': {'id': 3, 'type': 'location'} ... } 
{{'data': {'id': 4, 'type': 'location'} ... } 

... 

そして、あなたはidtypeの一意の組合せでのアイテムの数をカウントし、その結果を印刷したいですソートされた順序で?

あなたは一般的なカウント辞書パターン使用することができます

counts = dict() 
for item in data['data']: 
    # here I assume the items you are looking for are locations 
    # for it to be a key, it has to be immutable, so make it a tuple 
    val = item['relationships']['location']['data'] 
    location_tuple = (val['id'], val['type']) 
    if location_tuple in counts: 
     counts[location_tuple] += 1 
    else: 
     counts[location_tuple] = 1 

# print them out in order, first send to list of tuples and sort 
results = counts.items() 
results.sort() # will sort on first item, which will be id 

# results come in like so: ((3, location), 15) 
for item in results: 
    print 'id:', item[0][0], 'type:', item[0][1], 'count:' item[1] 

基本的な考え方ここではあなたがカウントしたいすべての明確な物事のキーとしてタプルを使用してカウントするために辞書を使用して、アイテムを使用することができるということですソートされたタプルのリストとして取得することができます。タプルは最初の要素、2番目の要素などで再帰的にソートされるので、タプルを作成して最初のソート・キーを最初の位置に置くなどの処理をすると、ソートを調整する必要がありますコール。あなたは、あなたが抽出して印刷したいものに依存しているものを調整しなければならないかもしれません。

1

データをデータベースに格納します(SQLiteなど)。次に "GROUP BY"を入力します。

関連する問題