将新元素添加到一个空列表中。

huangapple go评论64阅读模式
英文:

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.

Data showing (screenshot)

result_items[0]['Media'][0]['Description']

Result NaN (screenshot)

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[&#39;Media&#39;][0][&#39;Description&#39;]) 

huangapple
  • 本文由 发表于 2023年6月22日 01:14:32
  • 转载请务必保留本文链接:https://go.coder-hub.com/76525695.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定