在列中编写Python列表

时间:2015-04-10 23:14:26

标签: python list

如何在列中编写Python列表而不是连续编写?这显然是可能的,因为当一个人调用变量的comamnd dir()时,例如,它返回列中的列表。

>> a = [1, 2, 3]
>> dir(a)
Out[94]: 
['__add__',
 '__class__',
 '__contains__',
 '__delattr__',
 '__delitem__',
 '__dir__',
 '__doc__',
 '__eq__',
 '__format__',
 '__ge__',
 '__getattribute__',
 '__getitem__',
 '__gt__',
 '__hash__',
 '__iadd__',
 '__imul__',
 '__init__',
 '__iter__',
 '__le__',
 '__len__',
 '__lt__',
 '__mul__',
 '__ne__',
 '__new__',
 '__reduce__',
 '__reduce_ex__',
 '__repr__',
 '__reversed__',
 '__rmul__',
 '__setattr__',
 '__setitem__',
 '__sizeof__',
 '__str__',
 '__subclasshook__',
 'append',
 ...]

1 个答案:

答案 0 :(得分:1)

您可以使用python' Join函数从列表中创建一个字符串。 \n用于添加换行符,从而使其以列格式打印。

list  = ['a', 'b', 'c']
print('\n'.join(list))