列表的输出格式



在python中编写一段代码:

print (sorted([(r,len(f)) for r,d,f in os.walk(directory_path)],key=lambda x : x[1],reverse = True))

它给出的输出如下:

[('/etc/ssl/certs', 595), ('/etc/alternatives', 109), ('/etc/', 73), ('/etc/init.d', 52),('/etc/dovecot/conf.d', 27)]

我想要类似的东西(不将其存储到变量):

('/etc/ssl/certs', 595)
('/etc/alternatives', 109)
('/etc/', 73)
('/etc/init.d', 52)
('/etc/dovecot/conf.d', 27)]

使用 pprint

import pprint
pprint.pprint(sorted([(r,len(f)) for r,d,f in os.walk(directory_path)],key=lambda x : x[1],reverse = True))
pprint.pprint([('/etc/ssl/certs', 595), ('/etc/alternatives', 109), ('/etc/', 73), ('/etc/init.d', 52),('/etc/dovecot/conf.d', 27)])
# [('/etc/ssl/certs', 595),
#  ('/etc/alternatives', 109),
#  ('/etc/', 73),
#  ('/etc/init.d', 52),
#  ('/etc/dovecot/conf.d', 27)]

你试过这样的事情吗?

[print(str(x)) for x in sorted([(r,len(f)) for r,d,f in os.walk(directory_path)],key=lambda x : x[1],reverse = True)]

相关内容

  • 没有找到相关文章

最新更新