【问题标题】:How to solve this "TypeError" message in Python?如何在 Python 中解决此“TypeError”消息?
【发布时间】:2017-01-09 05:48:58
【问题描述】:

我是 Python 新手。我在 Jupyter Notebook 中运行以下代码行,我正在做散点图:

colors = ["#%02x%02x%02x" %(r,g,150) for r, g in zip(np.floor(50+2*x), np.floor(30+2*y))]

当我运行这段代码时,Python 会抛出以下错误消息:

TypeError                                 Traceback (most recent call last)
<ipython-input-9-ec6675e13d1f> in <module>()
----> 1 colors = ["#%02x%02x%02x" %(r,g,150) for r, g in zip(np.floor(50+2*x), np.floor(30+2*y))]

<ipython-input-9-ec6675e13d1f> in <listcomp>(.0)
----> 1 colors = ["#%02x%02x%02x" %(r,g,150) for r, g in zip(np.floor(50+2*x), np.floor(30+2*y))]

TypeError: %x format: an integer is required, not numpy.float64

是什么导致了这个错误,我该如何解决?

【问题讨论】:

  • 通过制作rg 整数?
  • 尝试["#%02x%02x%02x" %(int(r),int(g),150) for r, g in zip(np.floor(50+2*x), np.floor(30+2*y))]如果不起作用,请离开在线方法。
  • @dsgdfg 谢谢!工作精美!您可以将其发布为答案,以便我投票吗?
  • 但是,这个@AndrewLi 点不是我的!告诉他,对不起!
  • @AndrewLi 谢谢安德鲁,请作为答案发帖。

标签: python syntax typeerror jupyter-notebook


【解决方案1】:

感谢 Andrew Li 和“dsgdfg”为我指明了正确的方向。

将代码更改为以下内容即可:

["#%02x%02x%02x" %(int(r),int(g),150) for r, g in zip(np.floor(50+2*x), np.floor(30+2*y))]

【讨论】:

    猜你喜欢
    • 2016-02-20
    • 2014-12-24
    • 1970-01-01
    • 2014-02-20
    • 2011-09-10
    • 2020-02-28
    • 2017-10-14
    • 2015-06-29
    • 2020-07-29
    相关资源
    最近更新 更多