gpt4 book ai didi

python - 在mysql表中的不同行中插入字典的值

转载 作者:行者123 更新时间:2023-11-29 11:59:46 26 4
gpt4 key购买 nike

我有一个字典,每个键都有多个值。该字典是在解析日志文件后生成的。

([('RUNID', ['11098782', '11098782']), ('Test', ['OMP', 'AMP']), ('SubType', ['LU', 'BU']), ('Class', ['C', 'A']), ('Size', ['162', '64']), ('Iterations', ['250', '250']), ('Time', ['839.76', '44.30'])])

键的数量将相同,但与每个键相关的值可能 change(In the example above it is 2 and can be even more)我已经在 MySQL 中创建了一个表,其中列作为我的 Keys(Runid,Test,Subtype,Class,Size,Iterations,Time)字典的。现在我想插入 key values(11098782,OMP,LU,C,162,250,839.96)对应于数据库内单行中的每个键。与下一行相同但不同 values(1109872,AMP,BU,A,64,250,44.30)任何帮助表示赞赏。谢谢!!

最佳答案

您可以将列表转换为字典

然后 zip 字典的值以获得所需结果的列表。

注意:您应该修复您的sql,使按照的相应顺序排列。

>>> b = ([('RUNID', ['11098782', '11098782']), ('Test', ['OMP', 'AMP']), ('SubType', ['LU', 'BU']), ('Class', ['C', 'A']), ('Size', ['162', '64']), ('Iterations', ['250', '250']), ('Time', ['839.76', '44.30'])])
>>> b = dict(b)
>>> zip(*b.values())
[('LU', '11098782', '250', '839.76', 'OMP', 'C', '162'), ('BU', '11098782', '250', '44.30', 'AMP', 'A', '64')]
>>> b.keys()
['SubType', 'RUNID', 'Iterations', 'Time', 'Test', 'Class', 'Size'] # order matters

关于python - 在mysql表中的不同行中插入字典的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32581067/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com