使用python将列表转换为字符串
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/33629703/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Convert list to string using python
提问by Abdul Razak
I have the list it contain int ,float and string:
我有一个包含 int 、float 和 string 的列表:
lists = [10, "test", 10.5]
How Can i convert above list to string? I have tried:
如何将上面的列表转换为字符串?我试过了:
val = ','.join(lists)
print val
I am getting error like this:
我收到这样的错误:
sequence item 0: expected string, int found
How can I solve this issue?
我该如何解决这个问题?
采纳答案by SIslam
Firstly convert integers to string using str
using map
function then use join
function-
首先str
使用map
函数将整数转换为字符串,然后使用join
函数-
>>> ','.join(map(str,[10,"test",10.5]) )#since added comma inside the single quote output will be comma(,) separated
>>> '10,test,10.5'
Or if you want to convert each element of list into string then try-
或者,如果您想将列表的每个元素转换为字符串,请尝试-
>>> map(str,[10,"test",10.5])
>>> ['10', 'test', '10.5']
Or use itertools
for memory efficiency(large data)
或itertools
用于内存效率(大数据)
>>>from itertools import imap
>>>[i for i in imap(str,[10,"test",10.5])]
>>>['10', 'test', '10.5']
Or simply use list comprehension
或者简单地使用列表理解
>>>my_list=['10', 'test', 10.5]
>>>my_string_list=[str(i) for i in my_list]
>>>my_string_list
>>>['10', 'test', '10.5']
回答by Hackaholic
The error you are getting because join
wants elements to be string
type, but in your list there is integer
too, so 1st you have to convert them to type string.
您收到的错误是因为join
希望元素为string
类型,但在您的列表中也有integer
,因此首先您必须将它们转换为字符串类型。
you can use list comprehension and str and join to join them
您可以使用列表理解和 str 并加入它们
>>> lists = [10,"test",10.5]
>>> ",".join(str(x) for x in lists)
回答by Martijn Luyckx
If you want to convert each element in the list to a string, you could do it simply using a for-loop.
如果要将列表中的每个元素转换为字符串,只需使用 for 循环即可。
for i in range(len(lists)):
lists[i] = str(lists[i])
Alternatively, if you want to make one string where all elements are joined together, you could edit the code above slightly.
或者,如果您想制作一个所有元素连接在一起的字符串,您可以稍微编辑上面的代码。
string_of_lists = ""
for i in lists:
string_of_lists += str(i)
As you can tell, this is another way of doing it, apart from the other solutions using join.
正如您所看到的,除了使用 join 的其他解决方案之外,这是另一种方法。
I hope I helped!
我希望我有所帮助!
回答by COCO
You have to pass each item in your list as a string into the ','.join(sequence). Consider using:
您必须将列表中的每个项目作为字符串传递到 ','.join(sequence) 中。考虑使用:
val = ','.join([str(item) for item in lists])
val = ','.join([str(item) for item in list])
print val
打印值
回答by TigerhawkT3
The easiest way is to send the whole thing to str()
or repr()
:
最简单的方法是将整个内容发送到str()
or repr()
:
>>> lists = [10, "test", 10.5]
>>> str(lists)
"[10, 'test', 10.5]"
repr()
may produce a different result from str()
depending on what's defined for each type of object in the list
. The point of repr()
is that you can send such strings back to eval()
or ast.literal_eval()
to get the original object back:
repr()
可能会产生不同的结果,str()
具体取决于list
. 关键repr()
是您可以将此类字符串发送回eval()
或ast.literal_eval()
取回原始对象:
>>> import ast
>>> lists = [10, "test", 10.5]
>>> ast.literal_eval(repr(lists))
[10, 'test', 10.5]
回答by Shameem
This is also possible. Here x
variable is list.
这也是可能的。这里的x
变量是列表。
>>> '%s'*len(x) % tuple(x)
回答by Ashok
a = ['b','c','d']
strng = ''
for i in a:
strng +=str(i)
print strng
回答by m.zohary
回答by Prince Vijay
import functools
lists = [10,"test",10.5]
print(functools.reduce(lambda x,y:x+","+y,list(map(str,lists))))