【问题标题】:How can I do string formatting in a list?如何在列表中进行字符串格式化?
【发布时间】:2020-02-02 08:09:06
【问题描述】:

我正在使用 networkx,它只接受以下方式的输入:

G.add_edges_from( [ ( '%s' , '%s' ) ] , label = '%s'  % (items2[0] , items2[-1], anothervalue ))

根据我的搜索,问题可以总结为:

print (  ['%s']   %   'string'  )

显示的错误是:TypeError: unsupported operand type(s) for %: 'list' and 'str'

字符串格式不能在列表中使用。是否有任何解决方法,因为 networkx 输入只是列表格式。

基本上我希望发生这种情况:

G.add_edges_from( [ ( 'string1' , 'string2' ) ] , label = integar  % (items2[0] , items2[-1], anothervalue ))

【问题讨论】:

  • 你为什么认为你需要 %s?为什么不能直接使用这些值?您是否关注docs 或其他页面/教程?
  • 您说“它只接受以下方式的输入”。我从来没有使用看起来像这样的东西向networkx输入一些东西。你能解释一下items2 是什么吗?可能有更好的方法来创建图表。
  • @Joel items2 只是字符串。是的,肯定有。说 items2[0] 是“某物”,而 items2[-1] 是“其他东西”
  • 那么,为什么不G.add_edge( items2[0] , items2[-1])
  • 如果您解释一下为什么您希望它希望输入看起来像这样可能会有所帮助?

标签: string python-2.7 list typeerror networkx


【解决方案1】:

试试这个。这可能会解决您的问题。

print (  ['%s'   %   'string']  )

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-12
    • 1970-01-01
    • 1970-01-01
    • 2019-08-25
    相关资源
    最近更新 更多