Python で 以下のように、数値混じりのTuple を join して文字列化しようとしたところ、

elems = ('ham', 5, 1, 'bird')
",".join(elems)

---------------------------------------------------------------------------

TypeError                                 Traceback (most recent call last)

<ipython-input-5-cd41c12ae701> in <module>()
      1 elems = ('ham', 5, 1, 'bird')
----> 2 ",".join(elems)


TypeError: sequence item 1: expected string, int found

というエラーが発生しました。

対処法

[list - Why can’t I join this tuple in Python? - Stack Overflow
stackoverflowで上記の記事を発見し、記載されている通り、map 関数使い、str関数を挟み込むことで対処しました。

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

'ham,5,1,bird'

以上です。

コメント