Python在2D字典中附加值

时间:2019-03-14 20:10:27

标签: python dictionary 2d

我有以下词典,我想向'CorrectionsAll'追加一个数组。我尝试了附加选项,但无法获得所需的内容。有人可以帮我解释一下逻辑。

Dict1 = {'ShpmtID': 123, 'PickupDate': '2016/01/01 00:00:00', 'EstimatedDeliveryDate': '2016/01/10 00:00:00', 'OrigSic': 'LJB', 'DestSic': 'XCF', 'CorrectionHistory': [{'key': 405013, 'CorrectionsAll': [{'CorrChngDesc': 'Commodity Line Changed'}]}]}

想添加“折扣行已更改”,如下所示:

{'ShpmtID': 123, 'PickupDate': '2016/01/01 00:00:00', 'EstimatedDeliveryDate': '2016/01/10 00:00:00', 'OrigSic': 'LJB', 'DestSic': 'XCF', 'CorrectionHistory': [{'key': 405013, 'CorrectionsAll': [{'CorrChngDesc': 'Commodity Line Changed'}, {'CorrChngDesc': 'Discount Line Changed'}]}]}
  

复制评论:尝试了以下两个选项:

选项1:

Dict1["CorrectionHistory"]["CorrectionsAll"].append({'CorrChngDesc': 'Discount Line Changed'})  

选项2:

Dict1["CorrectionsAll"].append({'CorrChngDesc': 'Discount Line Changed'})

2 个答案:

答案 0 :(得分:2)

您只需访问所需列表,然后将其添加到该列表即可

Dict1['CorrectionHistory'][0]['CorrectionsAll'].append({'CorrChngDesc': 'Discount Line Changed'})

输出:

  

{'OrigSic':'LJB','PickupDate':'2016/01/01 00:00:00','DestSic':'XCF','ShpmtID':123,'CorrectionHistory':[{键”:405013,“ CorrectionsAll”:[{'CorrChngDesc:'Commodity Line Changed'},{'CorrChngDesc':'Discount Line Changed'}]}],'EstimatedDeliveryDate':'2016/01/10 00:00 :00'}

答案 1 :(得分:0)

  

我尝试了这两个选项:选项1:Dict1 [“ CorrectionHistory”] [“ CorrectionsAll”]。append({{'CorrChngDesc':'Discount Line Changed'})

选项1无效,因为import React, { Component } from 'react'; import './Task.scss'; let placeholder = document.createElement('li'); placeholder.className = 'placeholder'; class Task extends Component { constructor(props) { super(props); this.state = { ...props }; } dragStart(e) { this.dragged = e.currentTarget; e.dataTransfer.effectAllowed = 'move'; e.dataTransfer.setData('text/html', this.dragged); } dragEnd(e) { this.dragged.style.display = 'block'; this.dragged.parentNode.removeChild(placeholder); // Update state let data = this.state.tasks; let startPosition = Number(this.dragged.dataset.id); let endPosition = Number(this.over.dataset.id); if (startPosition < endPosition) endPosition--; data.splice(endPosition, 0, data.splice(startPosition, 1)[0]); this.setState({ tasks: data }); } dragOver(e) { e.preventDefault(); this.dragged.style.display = "none"; if (e.target.className === 'placeholder') return; this.over = e.target; e.target.parentNode.insertBefore(placeholder, e.target); } render() { let taskList = this.props.tasks.map((task, i) => { return ( <li className="task" data-id={i} key={i} draggable='true' onDragOver={this.dragOver.bind(this)} onDragEnd={(this.dragEnd.bind(this))} onDragStart={this.dragStart.bind(this)} > //These are are what don't work wrapped in other tags: {task.taskName} {task.taskBody} </li > ); }); return ( <ul className="taskList"> {taskList} </ul> ) } } export default Task; 不是"CorrectionAll"的元素,而是列表的元素。该列表不过是其中的一个元素。

"CorrectionHistory"
  

选项2:Dict1 [“ CorrectionsAll”]。append({'CorrChngDesc':'Discount Line Changed'})

选项2也不起作用,因为Dict1["CorrectionHistory"][0]["CorrectionsAll"] # Use [0] to go inside this list ! 不存在。再次,这不是直接原因,但我想您可以理解为什么。