【发布时间】:2018-05-31 12:28:29
【问题描述】:
我需要在python中做如下操作:
我有一个元组列表
data = [("John", 14, 12132.213, "Y", 34), ("Andrew", 23, 2121.21, "N", 66)]
我有一个字段列表:
fields = ["name", "age", "vol", "status", "limit"]
每个元组的数据按顺序对应每个字段。
我有一个字典
desc = { "name" : "string", "age" : "int", "vol" : "double", "status" : "byte", "limit" : "int" }
我需要生成一条以下列格式发送的消息:
[{"column": "name", "value": {"String": "John"}}, {"column": "age", "value": {"Int": 14}}, {"column": "vol", "value": {"Double": 12132.213}}, {"column": "status", "value": {"Byte": 89}}, {"column": "limit", "value": {"Int": 34}},
{"column": "name", "value": {"String": "Andrew"}}, {"column": "age", "value": {"Int": 23}}, {"column": "vol", "value": {"Double":2121.21}}, {"column": "status", "value": {"Byte": 78}}, {"column": "limit", "value": {"Int": 66}}]
我有两个函数可以生成这个:
def get_value(data_type, res):
if data_type == 'string':
return {'String' : res.strip()}
elif data_type == 'byte' :
return {'Byte' : ord(res[0])}
elif data_type == 'int':
return {'Int' : int(res)}
elif data_type == 'double':
return {'Double' : float(res)}
def generate_message(data, fields, desc):
result = []
for row in data:
for field, res in zip(fields, row):
data_type = desc[field]
val = {'column' : field,
'value' : get_value(data_type, res)}
result.append(val)
return result
但是,数据非常庞大,包含大量元组(约 200,000 个)。为它们中的每一个生成上述消息格式需要花费大量时间。有没有一种有效的方法来做到这一点。
P.S 需要这样的消息,因为我在队列上发送此消息,而消费者是需要类型信息的 C++ 客户端。
【问题讨论】:
-
为什么不直接发送第一个字典
desc作为模式,然后按原样发送数据,即将元数据交换与数据交换分开。 -
因此,在这种任务上,C++ 应该比 Python 更快
-
你真的需要构建 Python 数据结构,它是一个字典列表,其中一个字段是另一个字典,或者只是格式化为字符串?这会带来很大的不同。
标签: python performance optimization