简体   繁体   English

无法将其他字典列表中的字典附加到字典列表中

[英]Can't append a list of dicts with dict from another list of dicts

Been Googling hours again for answer but seems like my situation is unique, or my understanding of Py is...uh, unique. 再次搜索了几个小时,但似乎我的处境是独一无二的,或者我对Py的理解是...呃,独一无二。

I have a list of dictionaries (let's call it sqlListOfDict) built from MySQLdb.cursors.DictCursor query, Py print yields this: 我有一个从MySQLdb.cursors.DictCursor查询构建的字典列表(我们称它为sqlListOfDict),Py打印会产生以下结果:

[{'volYtday': 18083292,
  'volToday': 49395261,
  'time': datetime.timedelta(0, 34800),
  'priceChgPct': Decimal('1.2400'),
  'volDiffPct': 173.15414140301445,
  'priceNom': Decimal('6.5300'),
  'date': datetime.date(2013, 4, 30),
  'secCode': 'xoxoxo',
  'volDiff': 31311969}]

Then I have another list of dictionaries (let's call it myListOfDict), declared empty, Py print yields this: 然后,我有另一个字典列表(我们称其为myListOfDict),声明为空,Py打印得出此结果:

[{'priceChgPct': '',
  'volYtday': '',
  'priceNom': '',
  'volDiffPct': '',
  'volToday': '',
  'time': '',
  'date': '',
  'secCode': '',
  'volDiff': ''}]

I need to append myListOfDict with a specific dictionary from sqlListOfDict by index number, the code: 我需要通过索引号,代码从sqlListOfDict附加myListOfDict特定的字典:

myListOfDict.append(sqlListOfDict[0])

No joy, myListOfDict is still as declared (empty) and Py doesn't give any traceback. 不高兴,myListOfDict仍然是声明的(空),Py不提供任何回溯。

I did some experiments: 我做了一些实验:

listDictA = [dict.fromkeys(['secCode', 'volDiff'])]

listDictB = [{'secCode': 'valB1', 'volDiff': 1000},
             {'secCode': 'valB2', 'volDiff': 1000},
             {'secCode': 'valB3', 'volDiff': 1000},
             {'secCode': 'valB4', 'volDiff': 1000},
             {'secCode': 'valB5', 'volDiff': 1000},
             {'secCode': 'valA1', 'volDiff': 1000}]

Ran the same command: 运行相同的命令:

listDictA.append(listDictB[0])

It worked! 有效! What did I do wrong for the first case? 第一种情况我做错了什么? Thanks for helping. 感谢您的帮助。

It sounds like you're expecting that the first item in myListOfDict is filled up with values from the first entry in sqlListOfDict . 听起来好像您期望myListOfDict中的第一项被sqlListOfDict第一项的值填充。

You say that myListOfDict is declared empty , but the fact is that you don't need that kind of declaration. 您说myListOfDict 声明为空 ,但事实是您不需要这种声明。 In Python, you can append whatever objects you like to a list, without any declaration of what types go into it. 在Python中,您可以将所需的任何对象追加到列表中,而无需声明要放入的类型。

I'll simplify your example a bit, and hope that clears it up: 我将简化您的示例,并希望能将其清除:

>>> myListOfDict = []  # create an empty list
>>> sqlListOfDict = [{'volYtday': 18083292,
                      'volToday': 49395261}]
>>> myListOfDict.append(sqlListOfDict[0])
>>> myListOfDict
[{'volYtday': 18083292, 'volToday': 49395261}]

Also, if you're dealing with two lists, you might want to consider extending the first list with the second, instead of append only the first item. 另外,如果要处理两个列表,则可能要考虑用第二个扩展第一个列表,而不是仅追加第一个。

>>> myListOfDict.extend(sqlListOfDict)

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM