无法连接'str'和'list'对象列表包含整数

哈里比

我希望我的输出是:

"fileselection":[0, 1, 2, 3, 4, 5, 6, 7]

我的代码:

li=[0, 1, 2, 3, 4, 5, 6, 7]
st='"fileselection":'
print st+",".join(li)

当前正在抱怨:

TypeError: sequence item 0: expected string, int found.
ec

只需调用str()列表即可获得所需的输出:

>>> li = [0, 1, 2, 3, 4, 5, 6, 7]
>>> st = '"fileselection":'
>>> st + str(li)
'"fileselection":[0, 1, 2, 3, 4, 5, 6, 7]'

您看到的问题是因为的元素li是整数,要将它们连接到字符串,首先需要将它们转换为str您可以通过以下方式应用str()功能来实现map()

>>> st + ','.join(map(str, li))
'"fileselection":0,1,2,3,4,5,6,7'

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

Python:TypeError无法连接'str和'list'对象

Python TypeError:无法连接“ str”和“ list”对象

TypeError:无法连接“ str”和“ UUID”对象

openerp无法连接“ str”和“ function”对象

TypeError:无法连接“ str”和“ Redditor”对象

TypeError:无法连接电子邮件中的“ str”和“ list”对象

类型错误:无法连接“str”和“list”对象。如何将列表转换为字符串

TypeError:无法连接“ str”和“ float”对象:pandas

Python Selenium TypeError:无法连接“ str”和“ WebElement”对象

TypeError:无法连接“ str”和“ pygame.Surface”对象

Python:TypeError:无法连接“ str”和“ int”对象

re.search()TypeError:无法连接“ str”和“ NoneType”对象

TypeError:无法连接'str'和'function'对象的python文件

类型错误:无法连接“str”和“int”对象 1

rpsls游戏“(python)错误:无法连接'str'和'int'对象

Tkinter Label,TypeError:无法连接“ str”和“ instance”对象

类型错误无法连接“ str”和“ ErrorList”对象

abaqus脚本:TypeError:无法连接“ str”和“ Set”对象

目录列表到xml-无法连接'str'和'NoneType'对象-如何处理?

无法在 Python 中连接 str 和 int

Python:TypeError:无法连接“str”和“int”

如何连接str和int对象?

Python TypeError:无法在邮件的电子邮件正文部分中串联“ str”和“ list”对象

如何解决无法连接“ str”和“ file”对象的错误?

无法连接“ str”和“ int”对象,但可以通过自己的IDLE正常运行吗?

Python错误:无法连接“ str”和“ builtin_function_or_method”对象

类型错误:无法使用 haarcascade 在人脸检测中连接“str”和“int”对象

TypeError:无法连接类型为“ <class'str'>”的对象;仅Series和DataFrame objs有效

Python TypeError:尝试打印时无法连接“ str”和“ int”对象