Skip to content

Instantly share code, notes, and snippets.

@rupalbarman
Created March 29, 2017 05:57
Show Gist options
  • Save rupalbarman/947152801538f8b5531870f3f328c9d8 to your computer and use it in GitHub Desktop.
Save rupalbarman/947152801538f8b5531870f3f328c9d8 to your computer and use it in GitHub Desktop.
Basic one-liners in python 3 for reference
#generate all subsets of a sequence
f = lambda x: [[y for j, y in enumerate(set(x)) if (i >> j) & 1] for i in range(2**len(set(x)))]
print(f([1,2,3]))
#print((2>>1)&1) #1010-> 0101
# max() using lambda
maxx= lambda x,y: y if y>x else x
print(maxx(23,12))
# reduce a num to check whether it's in between the range 0-1
x=9000
x = sorted([0.0, x, 1.0])[1]
print(x)
# sum of positive digits in number
num=12345
sum_of_pos= sum(map(int, str(num)))
print(sum_of_pos)
#flattening list of lists into one lists
ll= [[1,2,3,4], [2,3,2], [90,6,332], [12, 989]]
print(ll)
l= [ele for sub in ll for ele in sub]
print(l)
# iteratior example
ass= '12abc'
it= iter(ass)
print(it, next(it), next(it), next(it))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment