Giter VIP home page Giter VIP logo

one-liners's Introduction

one-liners

A list of my daily used Python one liners (especially on CodinGame's Clash of Code)


User Input

Get two numbers from space-separated user input

# example: 1 2 -> x = 1, y = 2
x,y = map(int,input().split()) # can replace int by float

Get list of numbers from space-separated user input

# example: 1 2 3 4 5 -> nums = [1, 2, 3, 4, 5]
nums = list(map(int,input().split())) # can replace int by float

Get list of lists of numbers (matrix) from user input

# example:
# 3         # size of the matrix
# 1 2 3  ->  matrix = [[1, 2, 3],
# 4 5 6                [4, 5, 6],
# 7 8 9                [7, 8, 9]]
matrix = [list(map(int,input().split())) for _ in range(int(input()))] # can replace int by float

Other cases

# example: 1 2 3 -> x = 1, y = 3
x,_,y = map(int,input().split()) # we don't care about the second number

# example: 1 2 3 4 5 -> x = 1, y = 5
x,*_,y = map(int,input().split()) # we only care about the first and last numbers

Output

Print a list of numbers

# example: nums = [1, 2, 3, 4, 5] -> print 1 2 3 4 5
print(*nums) # can replace *nums

Print a matrix (list of lists)

# example:
# matrix = [[1, 2, 3], -> 1 2 3
#           [4, 5, 6],    4 5 6
#           [7, 8, 9]]    7 8 9
print('\n'.join(map(lambda x: ' '.join(map(str,x)), matrix)))

Other

Get distinct elements from an iterable (list, string ...) without changing order

# example: nums = [4, 1, 2, 3, 1, 4, 3] -> nums = [4, 1, 2, 3]
# example: string = 'abacbad' -> string = ['a', 'b', 'c', 'd'] # can use join to get a string
distinct = list(dict.fromkeys(my_iterable).keys())

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.