1. 程式人生 > >Python字典,列表,元組,字元的長度用len()函式,矩陣的長度用shape

Python字典,列表,元組,字元的長度用len()函式,矩陣的長度用shape

如果需要知道某資料型別的長度或維度,
先 print type(xxx)知道資料型別,例如讀取json檔案
[{“keypoints”: [ 677.9600219726562, 739.485595703125, 0.004566059447824955, 664.0691528320312, 747.8201293945312, 0.004143653437495232], “score”: 2.6802735328674316, “image_id”: “00500.jpg”, “category_id”: 1}, {“keypoints”: [ 456.440185546875, 1005.5248413085938, 0.0018323599360883236, 260.51141357421875, 846.6636352539062, 0.006010023411363363, 255.2160186767578, 1002.8771362304688, 0.004762142430990934], “score”: 2.4920735359191895, “image_id”: “00500.jpg”, “category_id”: 1}]

f = open("./3/alphapose-results.json", encoding='utf-8')  
setting = json.load(f)
print(type(setting))
<class 'list'>
print(len(setting))
2

對於另一個json檔案:
{“keypoints”: [ 677.9600219726562, 739.485595703125, 0.004566059447824955, 664.0691528320312, 747.8201293945312, 0.004143653437495232], “score”: 2.6802735328674316, “image_id”: “00500.jpg”, “category_id”: 1}

f = open("./3/alphapose-results.json", encoding='utf-8')  
setting = json.load(f)
print(type(setting))
<class 'dict'>
print(len(setting))
4