Python - 用逗号分隔列表 [英] Python - printing out list separated with comma

查看:692
本文介绍了Python - 用逗号分隔列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在写一段代码,输出用逗号分隔的项目列表。该列表是用for循环生成的。对于范围(5)中的x,我使用

 
print(x,end =,)

问题是我不知道如何去掉最后加上的逗号进入清单。它输出:

  0,1,2,3,4,

如何删除结尾处的','?

解决方案

< h2>通过 sep =,作为 print() 的一个参数

你几乎和印刷品有关。



不需要循环, print 有一个 sep 参数以及 end

 >>> print(* range(5),sep =,)
0,1,2,3,4



一点解释



print 内置函数将任意数量的项目作为参数打印。任何非关键字参数将被打印,由 sep 分开。 sep 的默认值是一个空格。

 >> ;> print(hello,world)
hello world

更改<$ c $

 >>>打印(hello,world,sep =cruel)
hello残酷的世界

每个参数都被字符串化为 str()。将一个迭代器传递给print语句会将迭代器作为一个参数进行串联。

 >>> print([hello,world],sep =cruel)
['hello','world']

但是,如果将星号放在迭代器前面,则会将其分解为单独的参数,并允许使用 sep

 >>> print(* [hello,world],sep =cruel)
hello cruel world

>>> print(* range(5),sep =---)
0 --- 1 --- 2 --- 3 --- 4
pre>

使用加入作为替代方案



用给定的分隔符将迭代器连接成字符串的另一种方法是使用分隔符字符串的 join 方法。



< pre $ >>> print(cruel.join([hello,world]))
hello cruel world

这有些笨拙,因为它要求将非字符串元素明确地转换为字符串。



pre $ >>> print(,。join([str(i)for i in range(5)]))
0,1 ,2,3,4



蛮力 - 非pythonic



你建议的方法是用一个循环连接一个字符串,并沿途添加逗号。


$ b

 >>>> iterable = range(5)
>>> result =
>>> for item,i in enumerate(iterable):
>>> result = result + str(item)
>>>如果我> len(iterable) - 1:
>>> result = result +,
>>> print(result)
0,1,2,3,4


I am writing a piece of code that should output a list of items separated with a comma. The list is generated with a for loop. I use the

for x in range(5):
    print(x, end=",")

The problem is I don't know how to get rid of the last comma that is added with the last entry in the list. It outputs this:

0,1,2,3,4,

How do I remove the ending ' , ' ?

解决方案

Pass sep="," as an argument to print()

You are nearly there with the print statement.

There is no need for a loop, print has a sep parameter as well as end.

>>> print(*range(5), sep=", ")
0, 1, 2, 3, 4

A little explanation

The print builtin takes any number of items as arguments to be printed. Any non-keyword arguments will be printed, separated by sep. The default value for sep is a single space.

>>> print("hello", "world")
hello world

Changing sep has the expected result.

>>> print("hello", "world", sep=" cruel ")
hello cruel world

Each argument is stringified as with str(). Passing an iterable to the print statement will stringify the iterable as one argument.

>>> print(["hello", "world"], sep=" cruel ")
['hello', 'world']

However, if you put the asterisk in front of your iterable this decomposes it into separate arguments and allows for the intended use of sep.

>>> print(*["hello", "world"], sep=" cruel ")
hello cruel world

>>> print(*range(5), sep="---")
0---1---2---3---4

Using join as an alternative

The alternative approach for joining an iterable into a string with a given separator is to use the join method of a separator string.

>>>print(" cruel ".join(["hello", "world"]))
hello cruel world

This is slightly clumsier because it requires non-string elements to be explicitly converted to strings.

>>>print(",".join([str(i) for i in range(5)]))
0,1,2,3,4

Brute force - non-pythonic

The approach you suggest is one where a loop is used to concatenate a string adding commas along the way. Of course this produces the correct result but its much harder work.

>>>iterable = range(5)
>>>result = ""
>>>for item, i in enumerate(iterable):
>>>    result = result + str(item)
>>>    if i > len(iterable) - 1:
>>>        result = result + ","
>>>print(result)
0,1,2,3,4

这篇关于Python - 用逗号分隔列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆