为什么我不能在 Python 中加入这个元组? [英] Why can't I join this tuple in Python?

查看:30
本文介绍了为什么我不能在 Python 中加入这个元组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

e = ('火腿', 5, 1, '鸟')logfile.write(','.join(e))

我必须加入它才能将其写入文本文件.

解决方案

join 只接受字符串列表,所以先转换它们

<预><代码>>>>e = ('火腿', 5, 1, '鸟')>>>','.join(map(str,e))'火腿,5,1,鸟'

或者更像pythonic

<预><代码>>>>','.join(str(i) for i in e)'火腿,5,1,鸟'

e = ('ham', 5, 1, 'bird')
logfile.write(','.join(e))

I have to join it so that I can write it into a text file.

解决方案

join only takes lists of strings, so convert them first

>>> e = ('ham', 5, 1, 'bird')
>>> ','.join(map(str,e))
'ham,5,1,bird'

Or maybe more pythonic

>>> ','.join(str(i) for i in e)
'ham,5,1,bird'

这篇关于为什么我不能在 Python 中加入这个元组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆