Skip to content

Instantly share code, notes, and snippets.

@rajmayank
Last active October 23, 2021 05:24
Show Gist options
  • Save rajmayank/3feb2a97e516c61ce13877cd5ef99c43 to your computer and use it in GitHub Desktop.
Save rajmayank/3feb2a97e516c61ce13877cd5ef99c43 to your computer and use it in GitHub Desktop.
Python Snippets
# ==== ==== ==== ==== ==== ==== ==== ==== ==== ==== ==== ==== ==== ==== ==== ====
# For a given list, find count of consecutive duplicates
from itertools import groupby
dupCount = [len(list(ListOfOccurrences)) for (element, ListOfOccurrences) in groupby(sortedList)]
# ==== ==== ==== ==== ==== ==== ==== ==== ==== ==== ==== ==== ==== ==== ==== ====
# Find permutations and combinations
from itertool import permutations, combinations
possiblePermutationsOfLenX = permutation(theList, X)
possibleCombinationssOfLenX = combinations(theList, X)
# ==== ==== ==== ==== ==== ==== ==== ==== ==== ==== ==== ==== ==== ==== ==== ====
# Converst a number into list of digits
digits = map(int, str(number)) # OR
digits = [int(x) for x in str(number)]
# ==== ==== ==== ==== ==== ==== ==== ==== ==== ==== ==== ==== ==== ==== ==== ====
# Flatten the List
import functools
nestedList = [[1,2,3], [4,5,6], [7], [8,9]]
functools.reduce(lambda x, y: x+y, nestedList) # [1, 2, 3, 4, 5, 6, 7, 8, 9]
# ==== ==== ==== ==== ==== ==== ==== ==== ==== ==== ==== ==== ==== ==== ==== ====
# Print all lower cased characters
import string
print(string.ascii_lowercase)
# ==== ==== ==== ==== ==== ==== ==== ==== ==== ==== ==== ==== ==== ==== ==== ====
# All values in list is True
all(myList)
# ==== ==== ==== ==== ==== ==== ==== ==== ==== ==== ==== ==== ==== ==== ==== ====
# Find distance between two points
import math
a = [2, 4]
b = [10, 20]
math.sqrt( (a[0] - b[0]) ** 2 + (a[1] - b[1]) ** 2 ) # OR
abs(complex(*a) - complex(*b))
# ==== ==== ==== ==== ==== ==== ==== ==== ==== ==== ==== ==== ==== ==== ==== ====
# Reverse a list
lst.reverse() #Inplace Reverse
lst[::-1]
reversed(lst)
# ==== ==== ==== ==== ==== ==== ==== ==== ==== ==== ==== ==== ==== ==== ==== ====
# Permutation, Combiation, Products
import itertools
# permutations('ABCD', 2) --> AB AC AD BA BC BD CA CB CD DA DB DC
# permutations(range(3)) --> 012 021 102 120 201 210
itertools.permutations(["ABBC", r=3) # ['ABB', 'ABC', 'ABB', 'ABC', 'ACB', 'ACB',
# 'BAB', 'BAC', 'BBA', 'BBC', 'BCA', 'BCB',
# 'BAB', 'BAC', 'BBA', 'BBC', 'BCA', 'BCB',
# 'CAB', 'CAB', 'CBA', 'CBB', 'CBA', 'CBB']
itertools.combinations(["ABBC", r=3) # ['ABB', 'ABC', 'ABC', 'BBC']
itertools.product(["a", "b"], [1, 2]) # [('a', 1), ('a', 2), ('b', 1), ('b', 2)]
# ==== ==== ==== ==== ==== ==== ==== ==== ==== ==== ==== ==== ==== ==== ==== ====
# ZIP
# arr = [0, 1, 2, 3, 4]
list(zip(arr, arr[1:])) # [(0, 1), (1, 2), (2, 3), (3, 4)]
# ==== ==== ==== ==== ==== ==== ==== ==== ==== ==== ==== ==== ==== ==== ==== ====
#
# ==== ==== ==== ==== ==== ==== ==== ==== ==== ==== ==== ==== ==== ==== ==== ====
#
# ==== ==== ==== ==== ==== ==== ==== ==== ==== ==== ==== ==== ==== ==== ==== ====
# #### #### #### #### #### FREAKING COOL SNIPPETS #### #### #### #### #### ####
# Assignment Operator
if (n := len(a)) > 10:
print(f"List is too long ({n} elements, expected <= 10)")
# Alternative to using the default dict
the_dict = {}
for x in my_list:
the_dict[x] = the_dict.get(x, 0) + 1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment