Python将元组列表转换为json
我有一个元组列表,如:Python将元组列表转换为json,python,python-2.7,dictionary,Python,Python 2.7,Dictionary,我有一个元组列表,如: [('a', '76', '20190208011713-0500'), ('b', '14', '20190208011713-0500'), ('c', '99', '20190208011713-0500'), ('d', '62', '20190208011713-0500'), ('e', '112', '20190208011713-0500'), ('f', '78', '20190208011713-0500'), ('g', '20', '2
[('a', '76', '20190208011713-0500'),
('b', '14', '20190208011713-0500'),
('c', '99', '20190208011713-0500'),
('d', '62', '20190208011713-0500'),
('e', '112', '20190208011713-0500'),
('f', '78', '20190208011713-0500'),
('g', '20', '20190208011713-0500'),
('h', '14', '20190208011713-0500'),
('i', '23', '20190208011713-0500'),
('a', '45', '20190208011803-0500'),
('b', '36', '20190208011803-0500'),
('c', '22', '20190208011803-0500'),
('d', '69', '20190208011803-0500'),
('e', '11', '20190208011803-0500'),
('f', '118', '20190208011803-0500'),
('g', '29', '20190208011803-0500'),
('h', '34', '20190208011803-0500'),
('i', '63', '20190208011803-0500')]
所有元组的第三个元素都是相同的。我正试图通过以下方式将此列表转换为字典:
{timestamp: 20190208011713-0500,
'a' : 76,
'b' : 14,
'c' : 99,
'd' : 62,
'e' : 112,
'f' : 78,
'g' : 20,
'h' : 14,
'i' : 23}
更新:
{'20190208011713-0500': {'a': '76',
'b': '14',
'c': '99',
'd': '62',
'e': '112',
'f': '78',
'g': '20',
'h': '14',
'i': '23'}}
到目前为止,我是手工操作的,但是我在一个列表中有大约1000个元组,具有相同的时间戳,但有时我也有不同的时间戳。我试图通过编写一个函数来自动化这个过程,因为我需要对多个元组列表反复使用代码来创建字典列表,但我不确定如何解决这个问题。如果有任何帮助,我们将不胜感激
[{'timestamp': '20190208011713-0500',
'a' : 76,
'b' : 14,
'c' : 99,
'd' : 62,
'e' : 112,
'f' : 78,
'g' : 20,
'h' : 14,
'i' : 23},
{'timestamp': '20190208011803-0500',
'a' : 45,
'b' : 36,
'c' : 22,
'd' : 69,
'e' : 11,
'f' : 118,
'g' : 29,
'h' : 34,
'i' : 63}]
如果时间戳保证相同,或者您不在乎它是否相同,那么只需使用:
result = {'timestamp': your_list[0][-1]}
result.update(tup[:2] for tup in your_list)
第二行获取每个元组的前两个元素,并将它们直接传递给。与类似,该方法接受一个包含(key,value)
元组的iterable,通过切片,这就是您已经拥有的
演示:
如果速度至关重要,则可以使用进行抓取,并在更新时使用应用:
from operator import itemgetter
try:
# forward compatibility with Python 3
from future_builtins import map
except ImportError:
pass
result = {'timestamp': your_list[0][-1]}
result.update(map(itemgetter(0, 1), your_list))
通过这些更改,整个result.update()
循环将在本机C代码中执行
如果不能保证时间戳是相同的,并且需要生成一个列表,则需要按时间戳拆分(分组)元组。如果输入至少已分组(具有相同时间戳的所有条目连续出现),则可以使用进行分组。然后构建每个词典的方法保持不变;我们也可以使用此处调用的itemgetter()
来生成分组键:
from itertools import groupby
from operator import itemgetter
try:
# forward compatibility with Python 3
from future_builtins import map
except ImportError:
pass
def group_timestamp(timestamp, group):
d = {'timestamp': timestamp}
d.update(map(itemgetter(0, 1), group))
return d
result = [group_timestamp(ts, group) for ts, group in groupby(your_list, itemgetter(2))]
如果它们没有分组,那么我就不会对输入进行排序。您希望使用字典将它们线性地分流到每个时间戳组中进行分组,然后在生成输出时对该字典的值进行排序:
groups = {}
for key, value, ts in your_list:
if ts not in groups:
groups[ts] = {'timestamp': ts}
groups[ts][key] = value
result = sorted(groups.values(), key=itemgetter('timestamp'))
这将按时间戳顺序提供分组的dicitonaries。如果输出顺序无关紧要,只需使用
result=list(groups.values())
说明更改的时间戳:
lst = [('a', '76', '20190208011713-0500'),
('b', '14', '20190208011713-0500'),
('c', '99', '20190208011713-0500'),
('d', '62', '20190208011713-0500'),
('e', '112', '20190208011713-0500'),
('f', '78', '20190208011713-0500'),
('g', '20', '20190208011713-0500'),
('h', '14', '20190208011713-0500'),
('i', '23', '20190208011713-0500')]
dict = {}
for tuple in lst:
if tuple[2] in dict:
dict[tuple[2]][tuple[0]] = tuple[1]
else:
dict[tuple[2]] = {}
dict[tuple[2]][tuple[0]] = tuple[1]
输出:
{'20190208011713-0500': {'a': '76',
'b': '14',
'c': '99',
'd': '62',
'e': '112',
'f': '78',
'g': '20',
'h': '14',
'i': '23'}}
我认为最简单的方法是:
return_dict = dict([item[:2] for item in your_list])
return_dict["timestamp"] = your_list[0][2]
旁注:Python2.7将在年达到其生命的终点。如果这是一个新项目,那么您真的应该在Python3中构建它。我下面的答案与Python2和Python3完全兼容,所以您可以随身携带。谢谢@Martijn,我会记住这一点。是的,我正在为这个项目使用python2.7,您说过我将把代码从python2迁移到python3。您的解决方案也可以使用:)上述代码可以正常工作,但只适用于相同的时间戳,而不适用于不同的时间戳。@akashbachu:这就是为什么答案的开头是:如果时间戳保证相同,@akashbachu:时间戳是否至少分组了?或者它们可以以任意顺序出现吗?它们以随机顺序出现。@akashbachu:对,我已经给出了它们何时分组和何时不分组的选项。
return_dict = dict([item[:2] for item in your_list])
return_dict["timestamp"] = your_list[0][2]