【问题标题】:TypeError: sequence index must be integer [duplicate]TypeError:序列索引必须是整数[重复]
【发布时间】:2015-05-23 01:42:36
【问题描述】:

我只是在每 3 个字符之间添加一个“,”时遇到问题。

print totalpoints
points = ','.join([totalpoints[i:i+3] for i in range(0, totalpoints, 3)])

输出:

875
TypeError: sequence index must be integer

【问题讨论】:

  • 你能详细说明一下吗? totalpoints 是什么?如果它不是字符串/list,你认为你会如何使用slice

标签: range python-2.5


【解决方案1】:

我不知道你实际上想做什么。但如果我没记错的话,下面会解决你的问题。

>>> totalpoints = 875123123 
>>> totalpoints = str(totalpoints)
>>> points = ','.join([totalpoints[i:i+3] for i in range(0, len(totalpoints), 3)])
>>> points
'875,123,123'

【讨论】:

  • 能否详细说明错误是什么,或者尽量减少您所做的更改?
猜你喜欢
  • 1970-01-01
  • 2018-01-21
  • 2018-09-16
  • 1970-01-01
  • 2019-03-14
  • 2021-06-03
  • 1970-01-01
  • 2018-01-03
  • 1970-01-01
相关资源
最近更新 更多