使列表成为字符串Python

使列表成为字符串Python

本文介绍了使列表成为字符串Python的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使列表数据成为字符串.我的清单数据是这样的:

I want to make list data to string.My list data like this :

[['data1'],['data2'],['data3']]

我想转换成这样的字符串:

I want to convert to string like this :

"[data1] [data2] [data3]"

我尝试像这样使用join:

I try to use join like this :

data=[['data1'],['data2'],['data3']]
list=" ".join(data)

但是会出现这样的错误:

But get error like this :

  string= " ".join(data)
TypeError: sequence item 0: expected string, list found

有人可以帮我吗?

推荐答案

根据您希望输出与样本相符的紧密程度,您有几种选择,以复杂度的升序显示:

Depending on how closely you want the output to conform to your sample, you have a few options, show here in ascending order of complexity:

>>> data=[['data1'],['data2'],['data3']]
>>> str(data)
"[['data1'], ['data2'], ['data3']]"
>>> ' '.join(map(str, data))
"['data1'] ['data2'] ['data3']"
>>> ' '.join(map(str, data)).replace("'", '')
'[data1] [data2] [data3]'

请记住,如果您给定的data样本与您的实际数据不匹配,则这些方法可能会或可能不会产生预期的结果.

Keep in mind that, if your given sample of data doesn't match your actual data, these methods may or may not produce the desired results.

这篇关于使列表成为字符串Python的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-20 09:30