英文:
Appending new elements into an empty list
问题
我在向列表中追加新元素时遇到问题。在第一个屏幕截图中显示了数据,这意味着数据存在,但当我在循环中追加数据时,显示的数据是NaN。请参考以下截图。
[显示的数据(屏幕截图)](https://i.stack.imgur.com/86U5d.png)
result_items[0]['Media'][0]['Description']
[结果为NaN(屏幕截图)](https://i.stack.imgur.com/HHzxi.png)
英文:
Im facing issue with appending new elements into list. In first SS the data is shown which means it is present but when Im appending the data in a loop, the data shown is Nan. Please refer to the screen shots.
result_items[0]['Media'][0]['Description']
for result in result_items:
try:
media_description.append(result[0]['Media'][0]['Description'])
except:
media_description.append(np.nan)
media_description
</details>
# 答案1
**得分**: 0
在你的第二张图片/代码单元中,你使用了 `result[0]`,然而 `result` 本身就是列表的元素,所以没有必要对元素进行下标引用。
用以下行替换第二个单元中的代码将解决这个问题:
```python
media_description.append(result['Media'][0]['Description'])
英文:
In your 2nd image/cell of code, you are using result[0]
whereas result
itself is the element of the list, so there was no need to subscript the element.
Replacing the line in 2nd cell with the following will fix the issue
media_description.append(result['Media'][0]['Description'])
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论