将Mysql元组重新映射到字典 [英] Remap Mysql tuple to dictionary
问题描述
你好
我想将Mysql的数据集转换为字典。
我自己制作了一些代码,想问你如果我这样做是正确的。
def remapmysql(a):
return(a [0],(a [1:]))
def test_map():
count = 100000#模拟记录数
l1 =范围(0,计数)
l2 =范围(计数,2 *计数)
l3 =范围(2 *计数,3 *计数)
z1 = zip(l1, l2,l3)#模拟一个mysql结果集
d1 = dict(map(remapmysql,z1))
返回d1
Hello
I want to convert a Mysql resulset to a dictionary.
I made some code myself, and want to ask you if I do this the right way.
def remapmysql(a):
return (a[0], (a[1:]))
def test_map():
count = 100000 # count of simulated records
l1 = range(0, count)
l2 = range(count , 2 * count )
l3 = range(2 * count, 3 * count )
z1 = zip(l1, l2, l3) # simulate a mysql resultset
d1 = dict(map(remapmysql,z1))
return d1
推荐答案
Pom写道:
Pom wrote:
我想将Mysql的数据集转换为字典。
I want to convert a Mysql resulset to a dictionary.
也就是说,你想将(key,value,...)元组的数组转换为包含键的
字典: (值,...)对,对吧?
that is, you want to convert an array of (key, value, ...) tuples to a
dictionary containing key: (value, ...) pairs, right ?
我自己制作了一些代码,想问你是否以正确的方式做到这一点。
def remapmysql(a):
返回(a [0],(a [1:]))
def test_map():
count = 100000#模拟记录数
l1 =范围(0,计数)
l2 =范围(计数, 2 *计数)
l3 =范围(2 *计数,3 *计数)
z1 = zip(l1,l2,l3)#simulate一个mysql结果集
d1 = dict(地图(remapmysql,z1))
返回d1
I made some code myself, and want to ask you if I do this the right way.
def remapmysql(a):
return (a[0], (a[1:]))
def test_map():
count = 100000 # count of simulated records
l1 = range(0, count)
l2 = range(count , 2 * count )
l3 = range(2 * count, 3 * count )
z1 = zip(l1, l2, l3) # simulate a mysql resultset
d1 = dict(map(remapmysql,z1))
return d1
对我来说很好看。
如果你关心性能,并且正在使用最近的Python,你可以尝试
尝试
d1 = dict((行[0],行[1:])对于z1中的行)
而不是,看看是否在运行速度更快(这使用生成器表达式
而不是回调和完整列表)。
< / F>
looks fine to me.
if you care about performance, and is using a recent Python, you could
try doing
d1 = dict((row[0], row[1:]) for row in z1)
instead, and see if that runs faster (this uses a generator expression
instead of a callback and a full list).
</F>
def remapmysql(a):
def remapmysql(a):
return(a [0],(a [1:]))
def test_map():
count = 100000#模拟记录数
l1 =范围(0,计数)
l2 =范围(计数,2 *计数)
l3 =范围(2 *计数,3 *计数)
z1 = zip(l1 ,l2,l3)#simulate一个mysql结果集
d1 = dict(map(remapmysql,z1))
返回d1
return (a[0], (a[1:]))
def test_map():
count = 100000 # count of simulated records
l1 = range(0, count)
l2 = range(count , 2 * count )
l3 = range(2 * count, 3 * count )
z1 = zip(l1, l2, l3) # simulate a mysql resultset
d1 = dict(map(remapmysql,z1))
return d1
我不确定map()是否需要,因为它可能只是
I''m not sure the map() is needed, as it could just be
>> d1 = dict((行[0],行[1:])对于z1中的行)
>>d1 = dict((row[0], row[1:]) for row in z1)
在我的测试中有效。
然而,要么似乎工作得相当好。
-tkc
which worked in my tests.
However either seems to work fairly well.
-tkc
Fredrik Lundh写道:
Fredrik Lundh wrote:
如果你关心性能,并且正在使用最近的Python,你可以
if you care about performance, and is using a recent Python, you could
是的我做; - )
yes i do ;-)
尝试做
d1 = dict(行[0],行[1:])行在z1)
代替,看看它是否运行得更快(这使用生成器表达式
而不是回调和完整列表)。
< / F>
try doing
d1 = dict((row[0], row[1:]) for row in z1)
instead, and see if that runs faster (this uses a generator expression
instead of a callback and a full list).
</F>
我更改了它并节省了一些时间让我这样离开!
,包含100000个测试记录:
I changed it and it saves me some time so I leave it like that!
with 100000 test records:
>>> ;
>>>
dict + map(1,2,3) - {1:(2,3)}:1.343秒。
dict + gen-expr(1,2,3) - {1:(2,3)}:0.861秒。
dict+map (1, 2, 3) -{1: (2, 3)}: 1.343 seconds.
dict+gen-expr (1, 2, 3) -{1: (2, 3)}: 0.861 seconds.
>>>
>>>
dict + map(1,2,3) - {1:(2,3)}:1.397秒。
dict + gen-expr(1,2,3) - {1:(2,3)}:0.943秒。
,包含500000个测试记录:
dict+map (1, 2, 3) -{1: (2, 3)}: 1.397 seconds.
dict+gen-expr (1, 2, 3) -{1: (2, 3)}: 0.943 seconds.
with 500000 test records:
>>>
>>>
dict + map(1,2,3) - {1:(2,3)}:13.297秒。
dict + gen-expr(1,2,3) - {1:(2,3)}:8.335秒。
dict+map (1, 2, 3) -{1: (2, 3)}: 13.297 seconds.
dict+gen-expr (1, 2, 3) -{1: (2, 3)}: 8.335 seconds.
>>>
>>>
dict + map(1,2,3) - {1:(2,3)}:14.548秒。 />
dict + gen-expr(1,2,3) - {1:(2,3)}:9.793秒。
dict+map (1, 2, 3) -{1: (2, 3)}: 14.548 seconds.
dict+gen-expr (1, 2, 3) -{1: (2, 3)}: 9.793 seconds.
>>>
>>>
谢谢!!
thank you!!
这篇关于将Mysql元组重新映射到字典的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!