2024-04-25 13:18:24 发布
网友
在python上用逗号分隔打印列表最干净的方法是什么。你知道吗
I have list: [1,2,3,4] I need: "1, 2, 3, 4"
你可以在这条线上做点什么。你知道吗
mylist = [1,2,3,4,5] m = map(str, mylist) # map function to convert each item in the list to a string j = ', '.join(m) # join to combine those strings with ', ' print (j)
您可以尝试使用map将str方法应用于所有要将int转换为str的项,然后使用join方法连接列表:
map
str
int
join
string.join
Concatenate a list or tuple of words with intervening occurrences of sep.
a=[1,2,3,4] print ",".join(map(str,a))
输出:
1,2,3,4
唯一不寻常的部分是...,不幸的是str和repr都变成了'Ellipsis'。其他答案不能正确处理,但这个答案:
...
repr
'Ellipsis'
>>> a = [1, 2, 3, 4, ...] >>> print(', '.join('...' if x is ... else str(x) for x in a)) 1, 2, 3, 4, ...
编辑:这是原来的问题,在它被改变之前(尽管它仍然有效)。你知道吗
你可以在这条线上做点什么。你知道吗
您可以尝试使用
map
将str
方法应用于所有要将int
转换为str
的项,然后使用join
方法连接列表:string.join
输出:
唯一不寻常的部分是
...
,不幸的是str
和repr
都变成了'Ellipsis'
。其他答案不能正确处理,但这个答案:编辑:这是原来的问题,在它被改变之前(尽管它仍然有效)。你知道吗
相关问题 更多 >
编程相关推荐