-3

在 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)]
4

2 回答 2

1

使用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)]
于 2017-10-24T09:54:38.720 回答
-1

你有没有尝试过这样的事情?

[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)]
于 2017-10-24T09:51:47.943 回答