首页 > 解决方案 > 如何在 python 中加入一个可迭代对象(字符串连接)?

问题描述

我看过其他有类似问题的帖子。

这是我的代码:

def fit_replace(text):
    text = np.array(text)
    text = ' '.join(text)
    return text

df['REPLACE'] = df['ISI'].apply(fit_replace)
df.head(5)

这是文本列表:

[['ini', 'juga', 'saya', 'sambil', 'disambi', 'kerja', 'masih', 'belajar',...

错误:

    <ipython-input-101-15e80ecfee7e> in fit_replace(text)
      1 def fit_replace(text):
      2     text = np.array(text)
----> 3     text = ' '.join(text)
      4     return text
      5 

TypeError: can only join an iterable

标签: pythonpython-3.x

解决方案


您需要先转换text为可迭代对象。

例如:

' '.join(list(text))

推荐阅读