Pythonã§ãã¼ã¿ãã¾ã¨ãã¦æ±ãã¨ãã«ã¯ã前回の一気通貫で簡単なグラフ描画含めて説明したようなリストという配列形式ãå©ç¨ã§ãã¾ããããããPythonã«ã¯ããã¨ã¯ç°ãªãç¹å¾´ã®ãã¼ã¿å½¢å¼ãããã¾ãããããDictã¨å¼ã°ããè¾æ¸åã§ãã
è¾æ¸åã便å©ãªç¨éã¯ããã¼ã¿ãããã¼ãã¨ãããªã¥ã¼ï¼å¤ï¼ãã®ã»ããã§æ±ãããã¨ãã§ããããã§ã¯ãPythonã®è¾æ¸åã«ã¤ãã¦ãåºæ¬ã®åºæ¬ãè¾æ¸åã«ãªã¹ãåã®é åãå ãããé層ã«ããè¾æ¸åãã¼ã¿ã®æ±ããããã«ã¯è¿å¹´æµè¡ãã®REST APIã§å¾ãJSONã®åºåãè¾æ¸åã«å¤æãã¦ç¹å®ã®å¤ã ããåãåºãã¦è¡¨ç¤ºããä¾ã¾ã§ãå°åºãã«ãããã¾ã¨ãã¦ä¸æ°ã«èª¬æãã¾ããç®æ¬¡ãè¦ã¦ãèªåã«å¿ è¦ãªã¨ããã ãèªãã§ããã ãã¦ãæ§ãã¾ããï¼å®è¡ããã¨ãã¯APIã®ã¨ãããé¤ãå é ãããã£ã¦ãã ããï¼ã
- è¾æ¸åãã¼ã¿ã®ä½æ
- ãã¼ã«ããè¾æ¸å ã®ãã¼ã¿æ¤ç´¢
- è¾æ¸ã«å¤ã追å
- è¾æ¸ã®ä¸ã®å¤ãå¤æ´
- è¾æ¸ã®ä¸ã®ãã¼ã¿åé¤
- ã½ã¼ããã¦è¡¨ç¤º
- è¾æ¸åã®ãã¼ã¿ããªã¹ãå½¢å¼ï¼é åï¼ã¸å¤æ
- è¾æ¸åã«ãªã¹ãå½¢å¼ï¼é åï¼ã®ãã¼ã¿ã追å
- è¾æ¸åã®ä¸ã®ãªã¹ãå½¢å¼ã®ãã¼ã¿ãåç §
- è¾æ¸ã®é層å(ãã¹ãå)
- é層åããè¾æ¸ãã¼ã¿ãåç §
- é層åããè¾æ¸ã«ãã¼ã¿ã追å
- é層ã«è¾æ¸ã追å
- Yahoo!Japanã®REST APIãå¼ã³åºãã¦ã¿ã
- æå¾ã«
è¾æ¸åãã¼ã¿ã®ä½æ
ãã£ã¨ãåºæ¬çãªè¾æ¸åãã¼ã¿ã®ä½ææ¹æ³ã§ãã{ }ã§å²ã£ã¦ãè¦ç´ ã®ãã¼ï¼ã©ãã«ã®ãããªãã®ï¼ã¨å¯¾å¿ããå¤ããã"ãã¼":å¤ãã®ã»ããã§ä¸¦ã¹ã¦ããã¾ãã
data1 = { "apple":1, "orange":2, "banana":3, "grape":4, "kiwi":5 } print (data1) {'apple': 1, 'orange': 2, 'banana': 3, 'grape': 4, 'kiwi': 5}
print(è¾æ¸å)以å¤ã«ã次ã®æ¹æ³ã§ãè¾æ¸ã®ãã¼ã¿ãå ¨ã¦è¡¨ç¤ºãããã¨ãã§ãã¾ããitems()ã¯è¾æ¸åãã¼ã¿ã®ãã¼ã¨å¤ã®ã»ãããæ±ãã¨ãã«ä½¿ãã¡ã½ããã§ãã
for i in data1.items(): print(i) ('apple', 1) ('orange', 2) ('banana', 3) ('grape', 4) ('kiwi', 5)
ã¡ãªã¿ã«ããã¼ã ããæ±ãããã¨ãã¯keys()ãå¤ã ããæ±ãããæã«ã¯values()ã使ãã¾ãã
for i in data1.keys(): print(i) apple orange banana grape kiwi
for i in data1.values(): print(i) 1 2 3 4 5
ãã¼ã«ããè¾æ¸å ã®ãã¼ã¿æ¤ç´¢
è¾æ¸å½¢å¼ã¯ããã¼ãç´¢å¼ä»£ããã«ä½¿ã£ã¦è©²å½ãããã¼ã«å¯¾å¿ããå¤ãæ¤ç´¢ãããã¨ãã§ãã¾ãã
print(data1["apple"]) 1
ãã¼ã«å¯¾å¿ããå¤ãåå¨ãããã©ããã調ã¹ããã¨ãã§ãã¾ããå¤ãã®ãã®ããããããã©ããã ããç¥ããããã¨ããç¨éã«ã¯æç¨ã§ãã
print("apple" in data1.keys()) True
print("melon" in data1.keys()) False
è¾æ¸ã«å¤ã追å
è¾æ¸ã«å¤ã追å ãããã¨ãã§ãã¾ãããã¼ã¨å¯¾å¿ããå¤ãæå®ãã¦ããã¾ãã
data1["strawberry"] = 6 print(data1) {'apple': 1, 'orange': 2, 'banana': 3, 'grape': 4, 'kiwi': 5, 'strawberry': 6}
è¾æ¸ã®ä¸ã®å¤ãå¤æ´
è¾æ¸ã®ãã¼ã¿ãå¤æ´ããã¨ãã¯ããã¼ã¨å¯¾å¿ããå¤ãæå®ãã¦ããã¾ããããã追å ã¨å ¨ãåãã§ããã¤ã¾ããæå®ãããã¼ãç¡ããã°è¿½å ãã¦ãããã°å¤æ´ãã¦ãã¾ãã¨ããä»æ§ã«ãªã£ã¦ãã¾ããè¾æ¸ã¯åããã¼ã®ãã®ãè¤æ°å ¥ãããã¨ãã§ããªãã®ã§ããããªã£ã¦ããã®ã§ãããã
data1["strawberry"] = 7 print(data1) {'apple': 1, 'orange': 2, 'banana': 3, 'grape': 4, 'kiwi': 5, 'strawberry': 7}
è¾æ¸ã®ä¸ã®ãã¼ã¿åé¤
è¾æ¸ã®ä¸ã®ãã¼ã¿ãåé¤ããã«ã¯ãpop()ã¨ããã¡ã½ããã使ãã()ã®ä¸ã«åé¤ããããã¼ã¿ã®ãã¼ãæå®ãã¾ãã
data1.pop("banana") print(data1) {'apple': 1, 'orange': 2, 'grape': 4, 'kiwi': 5, 'strawberry': 7}
ã½ã¼ããã¦è¡¨ç¤º
è¾æ¸åã®ãã¼ã¿ã¯é åºãä¿è¨¼ãã¾ããããããé åºããããã«ä¸¦ã¹æ¿ããããã°ãåºåæã«sorted()ã§ã½ã¼ããã¾ãã
print(sorted(data1.items())) [('apple', 1), ('grape', 4), ('kiwi', 5), ('orange', 2), ('strawberry', 7)]
éé ã«ä¸¦ã¹æ¿ããå ´åã¯ãreverse=Trueãæå®ãã¾ãã
print(sorted(data1.items(), reverse=True)) [('strawberry', 7), ('orange', 2), ('kiwi', 5), ('grape', 4), ('apple', 1)]
è¾æ¸åã®ãã¼ã¿ããªã¹ãå½¢å¼ï¼é åï¼ã¸å¤æ
è¾æ¸åã®ãã¼ã¿ããªã¹ãå½¢å¼ã«å¤æãã¦å ¥ãããã¨ãã§ãã¾ããlist()ã使ãã¾ãã
data2 = list(data1.items()) print(data2) [('apple', 1), ('orange', 2), ('grape', 4), ('kiwi', 5), ('strawberry', 7)]
è¾æ¸åã«ãªã¹ãå½¢å¼ï¼é åï¼ã®ãã¼ã¿ã追å
ãã¼ã«å¯¾å¿ãããå¤ã¯ãåå¥ã®å¤ã§ã¯ãªããé åã«ãããå ´åãèãããã¾ãããã®å ´åã¯ãè¾æ¸ã«ãªã¹ãå½¢å¼ã®ãã¼ã¿ã追å ãããã¨ãã§ãã¾ãã
data1["watermelon"] = [8, 9, 10] print(data1) {'apple': 1, 'orange': 2, 'grape': 4, 'kiwi': 5, 'strawberry': 7, 'watermelon': [8, 9, 10]}
è¾æ¸åã®ä¸ã®ãªã¹ãå½¢å¼ã®ãã¼ã¿ãåç §
é åã¨ãã¦ç»é²ãããªã¹ããããã¼ããæ¤ç´¢ãã¦åãåºãã¦è¡¨ç¤ºããã«ã¯ã¤ãã®ããã«ã§ãã¾ãã
print(data1["watermelon"]) [8, 9, 10]
ããã«ããã®ãªã¹ãã®ä¸ã¤ç®ã®è¦ç´ ã ããå¿ è¦ãã¨ããå ´åã¯æ¬¡ã®ããã«æå®ãã¾ãããªã¹ãå½¢å¼ã®å ´åãé åã®è¦ç´ ã®çªå·ã¯1ããã§ã¯ãªã0ããæ°ãã¾ãã
print(data1["watermelon"][0]) 8
è¾æ¸ã®é層å(ãã¹ãå)
è¾æ¸ã¯é層ã«ãããã¨ãã§ãã¾ããç« ç«ã¦ãä½ããããªæè¦ã§ãããããããã§ã¯ãï¼ã¤ã®æç©ãããã«ããfruitsãã¨ãããã¼ãã¤ãã¦ã²ã¨ã¾ã¨ãã«ã§ããããã«ãã¾ãã
{'fruits':
ã{'apple': 1,
ã'orange': 2,
ã'banana': 3,
ã'grape': 4,
ã'kiwi': 5}
}
data4 = {"fruits": { "apple":1, "orange":2, "banana":3, "grape":4, "kiwi":5 }} print(data4) {'fruits': {'apple': 1, 'orange': 2, 'banana': 3, 'grape': 4, 'kiwi': 5}}
é層åããè¾æ¸ãã¼ã¿ãåç §
ãã®ããã«é層åãããã¼ã¿ã®åç §ã¯ã©ãããã°ããã®ã§ãããï¼å®ã¯ãæåã®é層ã®ãã¼ã2層ç®ã®ãã¼ã®ãã¼ã¨ãä¸ä½ã®å±¤ããçµãè¾¼ãã§ããããé ã«ãã¼ã並ã¹ã¦ç¹å®ã§ããããã«ãã¦ããã°ãã®é çªã§ãã©ã£ã¦å¤ãåç §ã§ãã¾ããé層ãæ·±ããªã£ã¦ããã®ãã¿ã¼ã³ã¯åãã§ãã
print(data4["fruits"]["apple"]) 1
é層åããè¾æ¸ã«ãã¼ã¿ã追å
ããã§ã¯ãé層åããè¾æ¸ã¸ã®ãã¼ã¿è¿½å ã¯ã©ãããã°ããã®ã§ãããï¼ãããé層ããªãã¨ãã®ãã¿ã¼ã³ã¨åºæ¬çã«ã¯åããªã®ã§ãããè¾æ¸ã®ãã¼ã®æå®ã®ã¨ãããé層ã®æ°ã ã並ã¹ããã¨ã§ãã©ã£ã¦ç¹å®ã§ããããã«ãã¦ããã¾ãã
data4["fruits"]["lemon"] = 6 print(data4) {'fruits': {'apple': 1, 'orange': 2, 'banana': 3, 'grape': 4, 'kiwi': 5, 'lemon': 6}}
é層ã«è¾æ¸ã追å
ä¸è¨ã®ä¾ã¯ãfruitsã¨ããè¾æ¸ã ãã§ãããããã«ããã«vegetableã¨ããè¾æ¸åã®ãã¼ã¿ãã¾ããã¨è¿½å ãã¦ã¿ã¾ãããã
data4.update({'vegetables': {'carrot': 1, 'onion': 2, 'pumpkin': 3, 'potato': 4, 'parsley': 5}}) print(data4) {'fruits': {'apple': 1, 'orange': 2, 'banana': 3, 'grape': 4, 'kiwi': 5, 'lemon': 6}, 'vegetables': {'carrot': 1, 'onion': 2, 'pumpkin': 3, 'potato': 4, 'parsley': 5}}
Yahoo!Japanã®REST APIãå¼ã³åºãã¦ã¿ã
ããã§ã¯è¾æ¸åã®å¿ç¨ã¨ãã¦ãREST APIã®å¼ã³åºãã¨ãããããåå¾ããJSONå½¢å¼ã®ãã¼ã¿ãè¾æ¸åã«å¤æãã¦è¡¨ç¤ºãããã¨ãããç°¡åãªä¾ã示ãããã¨æãã¾ãã使ãã®ã¯ãYahoo!Japanãå
¬éãã¦ããAPIã§ããããããéµä¾¿çªå·ï¼ã105-0011ï¼ã«è©²å½ããä½ç½®æ
å ±ï¼ç·¯åº¦ã»çµåº¦ï¼ãå¼ã£å¼µãåºãã¾ããå°ããã®ããã°ã©ã ã®å®è¡ã«ã¯ãYahoo!Japanã®ããããããã¼ãããã¯ã¼ã¯ãã«ç»é²ãã¦åå¥ã®APIãã¼ãåå¾ããå¿
è¦ãããã¾ããAPIãã¼ã®åå¾ã¯åãªãæç¶ãã§é£ããããã¾ããã®ã§ããã®Webãµã¤ããããã©ã£ã¦åå¾ãã ããã
developer.yahoo.co.jp
ããã§ã¯ã¾ããrequestã¨jsonã®2ã¤ã®ã©ã¤ãã©ãªã¼ãimportããå¿ è¦ãããã¾ããåè ã¯URLã§ãã¼ã¿ãæ±ãããã®ãã®ãå¾è ã¯JSONå½¢å¼ãæ±ãããã®ãã®ã§ãã
import requests import json URL = "https://map.yahooapis.jp/search/zip/V1/zipCodeSearch?query=105-0011&output=json&appid=ï¼ããã¯åèªãYahoo! DeveloperããAPIãã¼ãåå¾ããå¿ è¦ãããã¾ãï¼" resp = requests.get(URL) dict= json.loads(resp.text) print("ä½ç½®æ å ±ï¼", dict['Feature'][0]['Geometry']['Coordinates']) ä½ç½®æ å ±ï¼ 139.74816650,35.65757726
API, JSON, è¾æ¸ã®ãã¹ãã¨ä¸è¦é£ãããã§ãããæ ¸ã¨ãªãé¨åã ããå縮ããã°ããã®ç¨åº¦ã®ã³ã¼ãéã§å®ç¾ã§ãã¾ããã¾ãURLãå¼ã³åºãã¦APIããåå¾ããJSONå½¢å¼ã®ãã¼ã¿ãè¾æ¸å½¢å¼ã«å¤æãã¦å ¥ãã¾ãããã¨ã¯ãããããæ·±ãé層ã«ããä½ç½®æ å ±ã®é¨åã ããé層ãã¨ã«ãã¼ã並ã¹ã¦ç¹å®ãã¦ããï¼ãªã¹ãã§å ¥ã£ã¦ããã¨ããã¯é åã®çªå·ã§æå®ãï¼ãprint()ã§ãã®å¤ãåºåãã¦ããã ãã§ãã
ã¡ãªã¿ã«ãJSONå½¢å¼ã§åå¾ãã¦è¾æ¸åã«å ¥ãããã¼ã¿å ¨ä½ã¯ä»¥ä¸ã«ãªãã¾ããï¼ç®ãããã¿ã¾ãï¼
{'ResultInfo': {'Count': 1, 'Total': 1, 'Start': 1, 'Status': 200, 'Description': '', 'Copyright': '', 'Latency': 0.009}, 'Feature': [{'Id': 'd30c42d8a8cb04ee730d1e5846ea2a8a', 'Gid': '', 'Name': 'ã105-0011', 'Geometry': {'Type': 'point', 'Coordinates': '139.74816650,35.65757726'}, 'Category': ['éµä¾¿çªå·', 'çºåéµä¾¿çªå·'], 'Description': 'Yahoo!éµä¾¿çªå·æ¤ç´¢', 'Style': [], 'Property': {'Uid': '2281d2dd75f52ee9ef0ba86149310b0cf710aedd', 'CassetteId': '3ee7f7f5fe1ef2267e319b15168e37d3', 'Country': {'Code': 'JP', 'Name': 'æ¥æ¬'}, 'Address': 'æ±äº¬é½æ¸¯åºèå ¬å', 'GovernmentCode': '13103', 'AddressMatchingLevel': '6', 'PostalName': 'æ±äº¬é½æ¸¯åºèå ¬å', 'Station': [{'Id': '22584', 'SubId': '2258401', 'Name': '御æé', 'Railway': 'é½å¶ä¸ç°ç·', 'Exit': 'A1', 'ExitId': '3607', 'Distance': '396', 'Time': '4', 'Geometry': {'Type': 'point', 'Coordinates': '139.750445,35.659440'}}, {'Id': '22712', 'SubId': '2271201', 'Name': 'èå ¬å', 'Railway': 'é½å¶ä¸ç°ç·', 'Exit': 'A4', 'ExitId': '4055', 'Distance': '496', 'Time': '6', 'Geometry': {'Type': 'point', 'Coordinates': '139.749697,35.654488'}}, {'Id': '22815', 'SubId': '2281501', 'Name': '大é(æ±äº¬é½)', 'Railway': 'é½å¶æµ èç·/é½å¶å¤§æ±æ¸ç·', 'Exit': 'A6', 'ExitId': '4541', 'Distance': '541', 'Time': '6', 'Geometry': {'Type': 'point', 'Coordinates': '139.753831,35.656964'}}]}}]}
æå¾ã«
è¾æ¸åã¯ãPythonã§ããã¼ãã¨ãããªã¥ã¼ãã§ãã¼ã¿ãæ±ãä¸ã§æ¬ ãããªããã¼ã¿å½¢å¼ã§ããç¾ä»£ã®ITã§ã¯ããã¼ã»ããªã¥ã¼ã»ã¹ãã¢(KVS: Key-Value Store)ã¨å¼ã°ããã¿ã¤ãã®NoSQLãã¼ã¿ãã¼ã¹ãä¸è¬åãã¦ãã¾ãããä¸è¨ã®ä¾ã§ç¤ºããããã«RSET APIãªã©ã§JSONã¨å¼ã°ããå½¢å¼ã®ãã¼ã¿ãå©ç¨ããã¨ãã«ãè¾æ¸åã«å¤æãã¦æ±ããã¨ãå¤ãããã¾ãããã£ã¦ãè¾æ¸åã¯Pythonãå¦ã¶ä¸ã§éè¦ãªåºç¤ç¥èã§ãããå®ç¨æ§ã®é«ããã¼ã¿å½¢å¼ã§ãããã¾ããããã§ã¯ããããä¸æ¬¡é層ã®è¾æ¸ã®åºæ¬ã ãã¨ããã®ã§ã¯ãªããé層åãJSONå½¢å¼ããã®å¤æã«ã¤ãã¦ãå®ä¾ã交ãã¦ã¾ã¨ãã¦èª¬æãã¦ã¿ã¾ããã®ã§ãåèã«ãªãã¨ãããããã°å¹¸ãã§ãã