我有两个字典如下。
A= {'photos': {'page': 1,
'pages': 2,
'perpage': 250,
'photo': [{'accuracy': '16',
'context': 0,
'datetaken': '2013-05-03 13:47:02',
'datetakengranularity': '0',
'datetakenunknown': '0',
'farm': 5,
'geo_is_contact': 0,
'geo_is_family': 0,
'geo_is_friend': 0,
'geo_is_public': 1,
'id': '26970758199',
'isfamily': 0,
'isfriend': 0,
'ispublic': 1,
'latitude': '5.944650',
'longitude': '80.459766',
'owner': '61943224@N04',
'place_id': 'kLamY9hTU7IVDlGv',
'secret': '1383a29a3b',
'server': '4563',
'title': 'Mirissa - Ask your Local Tour Dog',
'woeid': '2182931'},
{'accuracy': '14',
'context': 0,
'datetaken': '2017-09-28 18:13:17',
'datetakengranularity': 0,
'datetakenunknown': '1',
'farm': 5,
'geo_is_contact': 0,
'geo_is_family': 0,
'geo_is_friend': 0,
'geo_is_public': 1,
'id': '37343882352',
'isfamily': 0,
'isfriend': 0,
'ispublic': 1,
'latitude': '5.930026',
'longitude': '80.405502',
'owner': '7415626@N04',
'place_id': 'J1bv5NZTU70_mRm8',
'secret': 'c7135b74d2',
'server': '4335',
'title': 'Blue whale tail',
'woeid': '2179328'}],
'total': '2'},
'stat': 'ok'}
和第二个dictonary如下:
B={'photos': {'page': 2,
'pages': 2,
'perpage': 250,
'photo': [{'accuracy': '12',
'context': 0,
'datetaken': '2013-02-19 12:02:57',
'datetakengranularity': '0',
'datetakenunknown': 0,
'farm': 9,
'geo_is_contact': 0,
'geo_is_family': 0,
'geo_is_friend': 0,
'geo_is_public': 1,
'id': '8516011398',
'isfamily': 0,
'isfriend': 0,
'ispublic': 1,
'latitude': '5.935533',
'longitude': '80.446357',
'owner': '22857808@N03',
'place_id': 'kLamY9hTU7IVDlGv',
'secret': 'e6c6b8548c',
'server': '8112',
'title': 'Whale & Dolphin Watching Mirissa',
'woeid': '2182931'},
{'accuracy': '12',
'context': 0,
'datetaken': '2013-02-19 11:13:47',
'datetakengranularity': '0',
'datetakenunknown': 0,
'farm': 9,
'geo_is_contact': 0,
'geo_is_family': 0,
'geo_is_friend': 0,
'geo_is_public': 1,
'id': '8514896103',
'isfamily': 0,
'isfriend': 0,
'ispublic': 1,
'latitude': '5.935533',
'longitude': '80.446357',
'owner': '22857808@N03',
'place_id': 'kLamY9hTU7IVDlGv',
'secret': '17df7f97e7',
'server': '8375',
'title': 'Whale & Dolphin Watching Mirissa',
'woeid': '2182931'}],
'total': '2'},
'stat': 'ok'}
我想在删除
后合并这些dictonaries{'photos': {'page': '',
'pages': '',
'perpage': '',
并删除每个A和B词典的最后一部分,
'total': ''},
'stat': ''}
A和B中的部分。所以我的最终输出应该是这样的,
C='photo': [{'accuracy': '16',
'context': 0,
'datetaken': '2013-05-03 13:47:02',
'datetakengranularity': '0',
'datetakenunknown': '0',
'farm': 5,
'geo_is_contact': 0,
'geo_is_family': 0,
'geo_is_friend': 0,
'geo_is_public': 1,
'id': '26970758199',
'isfamily': 0,
'isfriend': 0,
'ispublic': 1,
'latitude': '5.944650',
'longitude': '80.459766',
'owner': '61943224@N04',
'place_id': 'kLamY9hTU7IVDlGv',
'secret': '1383a29a3b',
'server': '4563',
'title': 'Mirissa - Ask your Local Tour Dog',
'woeid': '2182931'},
{'accuracy': '14',
'context': 0,
'datetaken': '2017-09-28 18:13:17',
'datetakengranularity': 0,
'datetakenunknown': '1',
'farm': 5,
'geo_is_contact': 0,
'geo_is_family': 0,
'geo_is_friend': 0,
'geo_is_public': 1,
'id': '37343882352',
'isfamily': 0,
'isfriend': 0,
'ispublic': 1,
'latitude': '5.930026',
'longitude': '80.405502',
'owner': '7415626@N04',
'place_id': 'J1bv5NZTU70_mRm8',
'secret': 'c7135b74d2',
'server': '4335',
'title': 'Blue whale tail',
'woeid': '2179328'},
{'accuracy': '12',
'context': 0,
'datetaken': '2013-02-19 12:02:57',
'datetakengranularity': '0',
'datetakenunknown': 0,
'farm': 9,
'geo_is_contact': 0,
'geo_is_family': 0,
'geo_is_friend': 0,
'geo_is_public': 1,
'id': '8516011398',
'isfamily': 0,
'isfriend': 0,
'ispublic': 1,
'latitude': '5.935533',
'longitude': '80.446357',
'owner': '22857808@N03',
'place_id': 'kLamY9hTU7IVDlGv',
'secret': 'e6c6b8548c',
'server': '8112',
'title': 'Whale & Dolphin Watching Mirissa',
'woeid': '2182931'},
{'accuracy': '12',
'context': 0,
'datetaken': '2013-02-19 11:13:47',
'datetakengranularity': '0',
'datetakenunknown': 0,
'farm': 9,
'geo_is_contact': 0,
'geo_is_family': 0,
'geo_is_friend': 0,
'geo_is_public': 1,
'id': '8514896103',
'isfamily': 0,
'isfriend': 0,
'ispublic': 1,
'latitude': '5.935533',
'longitude': '80.446357',
'owner': '22857808@N03',
'place_id': 'kLamY9hTU7IVDlGv',
'secret': '17df7f97e7',
'server': '8375',
'title': 'Whale & Dolphin Watching Mirissa',
'woeid': '2182931'}]
我已经尝试了几个合并方法的词典,但没有一个产生结果,我对python有点新意。如果有人可以提供帮助,我将不胜感激。
答案 0 :(得分:0)
这是解决方案: -
(defun uniq-c (beginning end)
"Like M-| uniq -c"
(interactive "r")
(let ((source (current-buffer))
(dest (generate-new-buffer "*uniq-c*"))
(case-fold-search nil))
(set-buffer dest)
(insert-buffer-substring source beginning end)
(goto-char (point-min))
(while (let* ((line (buffer-substring (line-beginning-position)
(line-end-position)))
(pattern (concat "^" (regexp-quote line) "$"))
(count (count-matches pattern (point) (point-max))))
(insert (format "%d " count))
(forward-line 1)
(flush-lines pattern)
(not (eobp))))
(pop-to-buffer dest)))
答案 1 :(得分:0)
试试这个: -
C = A['photos']['photo']
C.extend(B['photos']['photo'])
D = {'photo': C}
print(D)
基本上你需要来自两个词典的项目photo
。由于此Item的值是一个列表,我们可以使用一个列表创建一个名为C的新列表,并扩展另一个列表。
参考: - Python List extend()