2016-08-28 105 views
0

我是Python的新手,並試圖找出使用列表理解解析JSON對象值到數組中的最佳方法。Python列表理解和JSON解析

這裏是我的代碼 - 我查詢公開可用的API iNaturalist並想借此JSON對象,它返回,所以我採取了JSON對象的特定部分成一條崎嶇不平的數組:

import json 
import urllib2 

#Set Observations URL request for Resplendent Quetzal of Costa Rica 
query = urllib2.urlopen("http://api.inaturalist.org/v1/observations?place_id=6924&taxon_id=20856&per_page=200&order=desc&order_by=created_at") 
obSet = json.load(query) 

#Print out Lat Long of observation 
n = obSet['total_results'] 

for i in range(n) : 
    print obSet['results'][i]['location'] 

這一切工作正常,並給出了以下的輸出:

9.5142456535,-83.8011438905 
10.2335478381,-84.8517773638 
10.3358965682,-84.9964271008 
10.3744851815,-84.9871494128 
10.2468720343,-84.9298072822 
... 

我想下一步要做的就是更換爲與列表理解循環,並存儲在一個元組中的位置值。我正在努力的語法,我猜這是這樣的:

[(long,lat) for i in range(n) for (long,lat) in obSet['results'][i]['location']] 

但這不工作......感謝任何幫助。

+1

該json中的位置是*字符串*。 –

+2

另外,由於你是Python新手,是否有任何特別的理由讓你使用Python 2,現在Python 3已經出現了** 8年**? –

+0

呃,優點!我可能最好把它弄清楚。 –

回答

1

obSet['results']是一個列表,無需使用range遍歷它:

for item in obSet['results']: 
    print(item['location']) 

使成列表理解這一點,你可以這樣寫:

[item['location'] for item in obSet['results']] 

但是,每個位置被編碼爲一個字符串,而不是浮點數的列表或元組。爲了得到它的正確格式,請使用

[tuple(float(coord) for coord in item['location'].split(',')) 
for item in obSet['results']] 

也就是說,拆分item['location']字符串轉換成使用,作爲分隔符的部分,然後每個部分轉換爲浮動,並且使這些浮動的元座標。

2

可以遍歷直接結果列表:

print([tuple(result['location'].split(',')) for result in obSet['results']]) 
>> [('9.5142456535', '-83.8011438905'), ('10.2335478381', '-84.8517773638'), ... ] 
+0

你正在返回一個字符串列表,而不是元組列表。 –

+0

'我接下來要做的是用列表理解替換for循環,並將位置值存儲在一個元組中。「 - 這是OP的要求。 –

+0

@AhsanulHaque固定。 – DeepSpace

2
[tuple(obSet['results'][i]['location'].split(',')) for i in range(n)] 

這將返回元組的列表,元組的元素是unicode

如果你想,作爲花車元組的元素,請執行下列操作:

[tuple(map(float,obSet['results'][i]['location'].split(','))) for i in range(n)] 
2

另一種方式來獲得的[長,緯度]列表中沒有列表理解:

In [14]: map(lambda x: obSet['results'][x]['location'].split(','), range(obSet['total_results'])) 
Out[14]: 
[[u'9.5142456535', u'-83.8011438905'], 
[u'10.2335478381', u'-84.8517773638'], 
[u'10.3358965682', u'-84.9964271008'], 
[u'10.3744851815', u'-84.9871494128'], 
... 

如果您想要替換元組列表:

In [14]: map(lambda x: tuple(obSet['results'][x]['location'].split(',')), range(obSet['total_results'])) 
Out[14]: 
[[u'9.5142456535', u'-83.8011438905'], 
[u'10.2335478381', u'-84.8517773638'], 
[u'10.3358965682', u'-84.9964271008'], 
[u'10.3744851815', u'-84.9871494128'], 
... 

如果你想轉換爲浮點數:

In [17]: map(lambda x: tuple(map(float, obSet['results'][x]['location'].split(','))), range(obSet['total_results'])) 
Out[17]: 
[(9.5142456535, -83.8011438905), 
(10.2335478381, -84.8517773638), 
(10.3358965682, -84.9964271008), 
(10.3744851815, -84.9871494128), 
(10.2468720343, -84.9298072822), 
(10.3456659939, -84.9451804822), 
... 
2

直接翻譯你的代碼到一個列表的理解是:

positions = [obSet['results'][i]['location'] for i in range(obSet['total_results'])] 

obSet['total_results']是言之有物,但沒有必要,你可以在obSet['results']只是循環而直接使用每個結果詞典:

positions = [res['location'] for res in obSet['results']] 

現在你有一個字符串列表但是,因爲每個'location'仍然是long,lat之前打印的格式化字符串。

拆分該字符串,並將結果轉換成浮動的序列:

positions = [map(float, res['location'].split(',')) for res in obSet['results']] 

現在你有名單的浮點值的列表:

>>> [map(float, res['location'].split(',')) for res in obSet['results']] 
[[9.5142456535, -83.8011438905], [10.2335478381, -84.8517773638], [10.3358965682, -84.9964271008], [10.3744851815, -84.9871494128], [10.2468720343, -84.9298072822], [10.3456659939, -84.9451804822], [10.3611732346, -84.9450302597], [10.3174360636, -84.8798676791], [10.325110706, -84.939710318], [9.4098152454, -83.9255607577], [9.4907141714, -83.9240819199], [9.562637289, -83.8170178428], [9.4373885911, -83.8312881263], [9.4766746409, -83.8120952573], [10.2651190176, -84.6360466565], [9.6572995298, -83.8322965118], [9.6997991784, -83.9076919066], [9.6811177044, -83.8487647156], [9.7416717045, -83.929327673], [9.4885099275, -83.9583968683], [10.1233252667, -84.5751029683], [9.4411815757, -83.824401543], [9.4202687169, -83.9550344212], [9.4620656621, -83.665183105], [9.5861809119, -83.8358881552], [9.4508914243, -83.9054016165], [9.4798058284, -83.9362558497], [9.5970449879, -83.8969131893], [9.5855562829, -83.8354434596], [10.2366179555, -84.854847472], [9.718459702, -83.8910277016], [9.4424384874, -83.8880459793], [9.5535916157, -83.9578166199], [10.4124554163, -84.9796942349], [10.0476688795, -84.298227929], [10.2129436252, -84.8384097435], [10.2052632717, -84.6053701877], [10.3835784147, -84.8677930134], [9.6079669672, -83.9084281155], [10.3583643315, -84.8069762134], [10.3975986735, -84.9196996767], [10.2060835381, -84.9698814407], [10.3322929317, -84.8805587129], [9.4756504472, -83.963818143], [10.3997876964, -84.9127311339], [10.1777433853, -84.0673088686], [10.3346128571, -84.9306278215], [9.5193346195, -83.9404786293], [9.421538224, -83.7689452093], [9.430427837, -83.9532672942], [10.3243212895, -84.9653175843], [10.021698503, -83.885674888]] 

如果必須有元組而不是名單,增加一個tuple()電話:

positions = [tuple(map(float, res['location'].split(','))) 
      for res in obSet['results']] 

後者還確保表達式在Python 3中工作(其中map()返回迭代器,而不是列表);否則你不得不使用嵌套列表理解:

# produce a list of lists in Python 3 
positions = [[float(p) for p in res['location'].split(',')] for res in obSet['results']] 
+0

感謝教授關於使用'map'的OP,當時我說服他嘗試了Python 3;) –

+0

@AnttiHaapala:好了,'tuple()'調用將很好地排序出來;-) –

1

要糾正的方式來獲得元組的列表應用列表解析是:

def to_tuple(coords_str): 
    return tuple(coords_str.split(',')) 

output_list = [to_tuple(obSet['results'][i]['location']) for i in range(obSet['total_results'])] 

當然,你可以將其替換to_tuple()一個lambda函數,我只是想讓這個例子清楚。此外,你可以使用map()有一個浮點數組而不是字符串:return tuple(map(float,coords_str.split(',')))

1

讓我們嘗試給這個一杆,從僅有1個位置:

>>> (long, lat) = obSet['results'][0]['location']

Traceback (most recent call last): 
    File "<stdin>", line 1, in <module> 
ValueError: too many values to unpack 

好了,沒有工作,但是爲什麼呢?這是因爲經緯度座標只有1個字符串,所以你不能立即將它作爲一個元組解包。我們必須首先將它分成兩個不同的字符串。

>>> (long, lat) = obSet['results'][0]['location'].split(",")

從這裏,我們會希望通過一整套的結果,我們知道從0索引到n的迭代。 tuple(obSet['results'][i]['location'].split(","))會給我們在索引i的結果的經度,緯度的元組,所以:
>>> [tuple(obSet['results'][i]['location'].split(",")) for i in range(n)]
應該給我們一組我們想要的元組。