在列中编写Python列表
如何在列中而不是在行中编写Python列表?这显然是可能的,因为当调用变量的comamnd dir()时,例如,该变量返回列中的列表在列中编写Python列表,python,list,Python,List,如何在列中而不是在行中编写Python列表?这显然是可能的,因为当调用变量的comamnd dir()时,例如,该变量返回列中的列表 >> a = [1, 2, 3] >> dir(a) Out[94]: ['__add__', '__class__', '__contains__', '__delattr__', '__delitem__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__'
>> 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',
...]
可以使用python函数从列表中创建字符串。\n
用于添加新行字符,从而使其以列格式打印
list = ['a', 'b', 'c']
print('\n'.join(list))
ipython显示
dir()
作为列输出。标准python交互会话不考虑模块。您所说的“写入”是什么意思?写入文件?或者打印到屏幕上?我的意思不是以列格式打印,而是将列表实际保存在表单中。看来这只是ipython的一种影响。那么,如何在ipython中以列的形式显示其他列表?例如一个普通的列表a=[1,2,3]pprint模块或多或少地完成了我需要的工作。谢谢