【问题标题】:Indexing a list when naming in a Python forloop在 Python forloop 中命名时索引列表
【发布时间】:2020-07-09 16:58:42
【问题描述】:

对于列表中的每个公司,如何将每个图表的标题设置为“公司”_price?

firm = ["A", "B", "C"]

for i in range(len(firm)):
    ...
    firm[i].plot(title = "{0}_price".format(firm[i]), ax=ax[0])

【问题讨论】:

  • 您需要在此处添加更多信息。 company 是什么? plot 是什么?看起来您正试图在字符串对象上调用 plot,这是无效的。
  • 谢谢;我已经修改了帖子。中间有一些代码。这些图表只是搞砸了“标题=”。目前我只能让它显示“company1_price”或一些变体作为标题。

标签: python list for-loop indexing format


【解决方案1】:

我认为部分问题在于您在 for 循环中更改了 firm 的定义,不建议这样做。

我猜你可能正在寻找这样的东西:

firms = ["A", "B", "C"]

for firm in firms:
    some_plot_var = ...
    some_plot_var.plot(title = "{f}_price".format(f=firm), ax=ax[0])

这应该导致标题为“A_price”、“B_Price”、“C_Price”。

【讨论】:

    【解决方案2】:

    简单但不一定最厚颜无耻的答案。

            for i in range(len(firm)): 
                ... 
                name = str(firm[i] + "_price") 
                plot(title = name, ...) 
    

    【讨论】:

    • UFuncTypeError: ufunc 'add' 不包含签名匹配类型的循环 (dtype(' dtype('
    猜你喜欢
    • 1970-01-01
    • 2022-01-18
    • 1970-01-01
    • 1970-01-01
    • 2013-11-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-07-17
    相关资源
    最近更新 更多