Last active
December 13, 2016 02:55
-
-
Save shoark7/0f0a23f13578963d3d2ce79fed02351e to your computer and use it in GitHub Desktop.
List prettify :)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| """ | |
| List prettify function proposed by "파이썬초짜" in my kakao open chatting room. | |
| """ | |
| import random | |
| import string | |
| sample_list = [random.choice(string.ascii_lowercase) * random.randint(4, 10) for _ in range(40)] | |
| def list_prettify(input_list, align=False): | |
| if align: | |
| max_length = len(sorted(input_list, key=lambda x: len(x), reverse=True)[0]) + 2 | |
| print( | |
| ''.join(['{:>{}}'.format(element, max_length) +',' if (i+1) % 5 != 0 \ | |
| else '{:>{}}'.format(element, max_length) + ',\n' \ | |
| for i, element in enumerate(sample_list)]) | |
| ) | |
| else: | |
| print( | |
| ''.join([element +',' if (i+1) % 5 != 0 else element + ',\n' for i, element in enumerate(sample_list)]) | |
| ) | |
| print('-' * 20) | |
| print('Prettify list with no alignment.') | |
| list_prettify(sample_list, align=False) | |
| print('-' * 20) | |
| print('Prettify list with alignment.') | |
| list_prettify(sample_list, align=True) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment