0
지능형리스트의 둥지 :어떻게 다음과 같은 목록 빌려 가지고
import subprocess
last_logins = [i.split(' ', 1)[0] for i in subprocess.check_output('last').split('\n') if 'reboot' not in i]
print("The last five logins were: {0}".format(', '.join(last_logins[0:5])))
을 그리고 반환
The last five logins were: vagrant, vagrant, vagrant, vagrant, vagrant
어떻게 둥지의 집계를 유지하는 하위 루프를 할 사용자는, 그리고이 경우, 반환
는The last five logins were: vagrant: 2x, some_other_user: 3x
예, 그것은 추한과 이해 라인이 너무 깁니다. (나는 더 명확한 코드 작성을위한 비판/제안에 개방적이다.) 그러나 (목록) 내포에 루프를 중첩시키는 올바른 방법을 찾고 싶습니다.
무엇이야 정상적인 루프에 문제가 있습니까? –
아무 것도. 목록 보급/원 - 라이너를 사용하는 방법에 대해서도 궁금합니다. – MagicToaster