简体   繁体   English

将两个 JSON {key1:value1 , key2:value2} 组合成单个键(即 {key3:value1,value2})Python

[英]combining two JSON {key1:value1 , key2:value2} to single key (i e {key3:value1,value2}) Python

I want to combine longitude and latitude to {latlon: '40.33333,-79.34343'}我想将经度和纬度结合到 {latlon: '40.33333,-79.34343'}

the entire JSON is in variable data = jsonData整个 JSON 在变量 data = jsonData 中

I want to remove original key-value pair我想删除原始键值对

{
  'locale': 'en_US',
  'timezone': '-7',
  'id': '13',
  'agerangemin': '21',
  'verified': 'true',
  'coverimageurl': 'scontent.xx.fbcdn/t31.0-0/p480x480/13063482_1183967848280764_1411489384515766669_o.jpg',
  'tagline': 'Veggien',
  'lastupdated': '1462341401',
  'fbupdated_time': '2016-03-30T00:38:48+0000',
  'lname': 'Kulkarni',
  'fname': 'Nikhil',
  'email': 'nikhilhk.usa@gmail.com',
  'latitude': '40.333333',
  'longitude': '-79.34343',
  'displayname': 'Nikhil Kulkarni',
  'fbprofileid': '1121344884543061',
  'profileimageurl': 'scontent.xx.fbcdn/hprofile-xft1/v/t1.0-1/p100x100/10423743_952350738109144_964810479230145631_n.jpg?oh=71f7e953dbbf8e2f1d9f22418f7888b2&oe=579F4A36',
  'link': 'facebook/app_scoped_user_id/1121344884543061/',
  'diet': 'Vegetarian',
  'dietsinceyear': '1966',
  'gender': 'M',
  'vegstory': '',
  'shortdescription': 'Just like that',
  'categoryids': '',
  'reasonforveg': 'Religious'
}
>>> json_data['latlon'] = ','.join(json_data[k] for k in ('latitude', 'longitude'))
>>> json_data['latlon']
'40.333333,-79.34343'

Note that this will preserve the original key-value pair.请注意,这将保留原始键值对。


UPDATE:更新:

If you want to remove the original key-value pair use pop method:如果要删除原始键值对,请使用pop方法:

>>> json_data['latlon'] = ','.join(json_data.pop(k) for k in ('latitude', 'longitude'))
>>> json_data['latlon']
'40.333333,-79.34343'
data['latlong'] = data['latitude'] + ',' + data['longitude']
del data['latitude']
del data['longitude']

Can be done in one line.可以在一行中完成。

>>> dic = {'latitude': '40.333333', 'longitude': '-79.34343'}
>>>
>>> dic['latlon'] = "{0},{1}".format(dic.pop('latitude'),dic.pop('longitude'))
>>> dic
{'latlon': '40.333333,-79.34343'}

To understand how dic.pop() work, see this.要了解dic.pop()工作原理,请参阅此内容。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在Python中将(键,值)(键,值)映射到(键,(值1,值2)) - Mapping (key, value) (key, value) to (key, (value1, value2)) in Python 从 {key1: [value1, value2...]} 到 {value1: [key1, key2...]} 的有效方法 - Efficient way to get from {key1: [value1, value2...]} to {value1: [key1, key2...]} 从字典 1 中提取 key1:value1 和从字典 2 中提取 key1:value1,将它们分配给 4 个不同的变量,循环 - Extract key1:value1 from dictionary 1 and key1:value1 from dictionary 2, assign them to 4 different variables, loop 如何将字典的格式更改为value1; key1; 在Python 3.6中? - How do I change the format of a dictionary to value1;key1; in Python 3.6? 了解“对于xx中的value1,value2” - Understanding “for value1,value2 in xx” 这叫做什么:myVar = value1或value2 - What is this called: myVar = value1 or value2 如果value1 == value2在python中不是None,比较如何工作? - How does compare work if value1 == value2 is not None in python? 使用key = value1,... valuen格式在python中编写文件 - Writing a file in python in a key=value1,…valuen format Pandas DataFrame 在“Value2”中加入/合并“Value1” - Pandas DataFrame join/merge on “Value1” in “Value2” 文本文件的Python脚本-字符串,“ [[value1,value2,value3]” - Python script for text file - string, “[value1, value2, value3]”
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM