From 449697e030ea8b4e0fca3048b7922de571628ac9 Mon Sep 17 00:00:00 2001 From: Nameless God <2582347430@qq.com> Date: Fri, 20 Jul 2018 10:57:23 +0800 Subject: [PATCH] add Python 1line --- sheets/_python/1line | 59 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 59 insertions(+) create mode 100644 sheets/_python/1line diff --git a/sheets/_python/1line b/sheets/_python/1line new file mode 100644 index 0000000..2517a1d --- /dev/null +++ b/sheets/_python/1line @@ -0,0 +1,59 @@ +# +# BASIC DATA STRUCTURE +# ------------------------- + +# Swap two numbers +a, b = b, a + +# List flattening +print(list(itertools.chain(*my_list))) + +# Merge two dicts +[**d1, **d2] + +# Reverse key, value in a dict +{v: k for k, v in d.items()} + +# Transpose a matrix +list(zip(*matrix)) + +# +# OOP +# ------------------------- + +# One-Line Constructors +class A(object): + def __init__(self, a, b, c, d, e, f): + self.__dict__.update({k: v for k, v in locals().items() if k != 'self'}) + +# +# FILE MANIPULATION +# ------------------------- + +# Reading lines as arrays from a file +lines = [line.strip() for line in open('foo.txt')] + +# Read rows of a csv file +with open('foo.csv', 'r') as f: rows = [line.strip().split(',') for line in f.readlines()] + +# CSV file to json +python -c "import csv,json;print(json.dumps(list(csv.reader(open('2017.csv')))))" + +# +# MISC +# ------------------------- + +# Print a 9 * 9 multiplication table +print('\n'.join([' '.join([f'{x}*{y}={x*y}' for x in range(1,y+1)]) for y in range(1,10)])) + +# Print a heart +print('\n'.join([''.join([('OhMyLove'[(x-y)%8]if((x*0.05)**2+(y*0.1)**2-1)**3-(x*0.05)**2*(y*0.1)**3<=0 else' ')for x in range(-30,30)])for y in range(15,-15,-1)])) + +# Generate an infinite maze +python -c "while 1:import random;print(random.choice('|| __'), end='')" + +# Simulate a slot machine +python -c "import random;p=lambda:random.choice('7♪♫♣♠♦♥◄☼☽');[print('|'.join([p(),p(),p()]),end='\r') for i in range(8**5)]" + +# Guess number game +python -c "import random;n=random.randint(1,99);[(lambda a:print('Y' if a==n else 'H' if a>n else 'L'))(int(input())) for i in range(6)]" \ No newline at end of file